给定以下代码,我在弄清楚如何设置原型链时遇到了一些麻烦。
var Model = {
prototype: {
init: function(){},
log: function(){ console.log('instance method log was called') }
},
log: function(){ console.log('class method log was called') },
create: function() {
var object = Object.create(this);
object.parent = this;
object.prototype = object.fn = Object.create(this.prototype);
return object;
},
init: function() {
var instance = Object.create(this.prototype);
instance.parent = this;
instance.init.apply(instance, arguments);
return instance;
}
}
var User = Model.create();
User.log(); // 'class method log was called'
// create a new method after creation
Model.warn = function() { console.warn('warn was called') }
User.warn() // 'warn was called'
var user = User.init();
user.log(); // 'instance method log was called'
具体来说,这一行让我在 create 方法中感到困惑:
object.prototype = object.fn = Object.create(this.prototype);
我了解 create 方法如何创建一个原型指向 Model 的新对象,但倒数第二行似乎用新对象 (Model.prototype) 覆盖了该原型。但是,看起来原始原型仍然完好无损,因为即使在创建新对象后我也可以向 Model 添加方法,并且新对象仍然可以访问它。
有人可以对实际发生的事情有所了解吗?
编辑- 我应该指出这段代码来自O'reilly的Javascript Web Applications