在那边《 JavaScript 编程精髓》里面的例子。
代码: https://output.jsbin.com/zupezayihi
var print = console.log;
function BasicServer() {
this.decorator_list = []
this.decorators = {}
}
BasicServer.prototype.init = function() {
for (var i = 0; i < this.decorator_list.length; i++) {
var func = this.decorator_list[i]
this.decorators[func].init(i)
}
}
BasicServer.prototype.decorated = function(i) {
this.decorator_list.push(i)
}
//BasicServer.prototype.decorators = {}
BasicServer.prototype.decorators.nodeServer = {
init: function(i) {
print('init node Server');
print(i);
}
}
为什么我在函数里面定义的变量
this.decorators = {}
在 BasicServer.prototype.decorators.nodeServer 添加元素的时候会提示 nodeServer undefine ?
用注释掉的方法就可以, BasicServer.prototype.decorators = {}
对 prototype 这些术语不太熟,可能问的不太明白,见谅了~