返回列表

Javascript对象真经

默认分类 2011-01-16 19:14:48

内部原型链与外部人为构造维护的原型链

这幅图还配套了代码呢。

[javascript]
function MyObject() {
    this.constructor = arguments.callee; //正确维护constructor,以便回溯外部原型链
}
MyObject.prototype = new Object(); //人为构建外部原型链

function MyObjectEx() {
    this.constructor = arguments.callee; //正确维护constructor,以便回溯外部原型链
}
MyObjectEx.prototype = new MyObject(); //人为构建外部原型链

obj1 = new MyObjectEx();
obj2 = new MyObjectEx();

print(obj1.constructor === MyObjectEx); //true
print(MyObjectEx.prototype instanceof MyObject); //true
print(MyObjectEx.prototype.constructor === MyObject); //true
print(MyObject.prototype instanceof Object); //true
print(MyObject.prototype.constructor === Object); //true
//完成了所有的回溯
print(obj1.constructor.prototype.constructor.prototype.constructor === Object); //true
[/javascript]

http://haiyang.me/javascript/js_prototype_constructor.htm 转自:http://hjp.im/javascript/master-javascript-object-system/