原型方法未被覆盖
Prototype method not being overriden
为什么控制台中没有记录foo
?我假设foo
基方法将被子方法覆盖。为什么不是这样?
function parent(){ }
parent.prototype.foo = function(){
console.log('foobar');
};
function child(){ }
child.prototype.foo = function(){
console.log('foo');
};
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
console.log(new child().foo()); // foobar
进行时
child.prototype = Object.create(parent.prototype)
替换先前添加了foo
特性的对象。
只需更改顺序即可稍后设置foo
值:
function parent(){ }
parent.prototype.foo = function(){
console.log('foobar');
};
function child(){ }
child.prototype = Object.create(parent.prototype);
child.prototype.foo = function(){
console.log('foo');
};
child.prototype.constructor = child;
console.log(new child().foo()); // foo
相关文章:
- 如何覆盖主干中的extend方法
- 使用通用方法覆盖每个组合getValue字段
- 如何在循环中使用jQuery.on()方法,而不覆盖我循环的对象
- Javascript集:任何覆盖元素之间比较的方法
- 覆盖 WebResource javascript 方法 - WebForm_InitCallback()
- ES2015:从覆盖的类调用内部方法
- 是否可以检测父方法是否被覆盖
- 有没有任何方法可以覆盖浏览器渲染页面的方式
- 在IE8中恢复被覆盖的javascript方法
- javascript原生原型:扩展、添加和覆盖方法
- 覆盖原型方法
- Javascript 覆盖对象的方法
- XrayWrapper 错误(值是可调用的)在 Firefox 插件中覆盖原型方法时
- 在 ES6 中调用覆盖方法库
- jquery widget 中的覆盖方法
- 从不同的.js文件中访问 1 个.js源文件 (jqTree) 中的变量以覆盖方法
- 覆盖方法的 JavaScript 原型调用基方法
- 使用锚标记在 nodeJS 中覆盖方法 GET 以删除
- JavaScript 中的覆盖方法
- 如何使用本机(未覆盖)方法