似乎我又一次没有正确理解原型.
如果你在一个对象上调用了一个方法,但它不存在——它不判断该方法是否可以运行它的原型吗?
喜欢
Array.prototype.myArrayMagic = function () { ... }
那么在任何数组上都可以调用arr.myArrayMagic()?
这是我的理解,但我现在遇到了一个问题,即除非我显式地调用原型方法,否则调用原型方法将无法工作.原型
Also: I am restricted to ES5 code
基本模块设置
function Wall() {}
Wall.prototype = {
shouldShowWall: function() {
return true;
},
show: function () {
if (!this.shouldShowWall()) {
return;
}
// Do other stuff here
}
}
module.exports = Wall;
子对象
function OtherWall() {
this.item = 'my_tracking_id',
this.minutes = 30
// ...
}
OtherWall.prototype = {
constructor: Object.create(Wall.prototype),
/**
* Override wall.prototype.shouldShowWall method for deciding if the wall should be shown at this time
*/
shouldShowWall: function() {
// return true or flase here
}.bind(this)
}
module.exports = OtherWall;
因此,遵循其他答案并尽可能地理解,这应该以一种我可以调用的方式设置
new OtherWall().show();
但它不起作用,只有我打电话才能起作用
new OtherWall().prototype.show();
这正常吗,还是我设置错了?
我需要改变什么才能让它与OtherWall().show();
一起工作