var console = {};
console.log = alert;
function MyClass(name) {
this.name = name;
};
MyClass.prototype.getName = function() {
return this.name;
};
var p = new MyClass("ZhangSan");
console.log(p.constructor); // MyClass
console.log(p.constructor.constructor); // Function
console.log(MyClass.constructor); // Function,此处等价于上面一种情况,因为p.constructor === Persion
console.log(MyClass.prototype.constructor); // MyClass
console.log(p.constructor.prototype.constructor); // MyClass,此处与上面等价,同样因为p.constructor === Persion
//MyClass.prototype = {} 等价于 MyClass.prototype = new Object()
MyClass.prototype = {
getName: function() {
return this.name;
}
};
var q = new MyClass("ZhangSan");
console.log(q.constructor); // Object
console.log(MyClass.constructor); // Function
console.log(MyClass.prototype.constructor); // Object,对prototype赋值导致prototype中的constructor成员被Object中的同名属性覆盖
console.log(q.constructor.prototype.constructor);
|