constructor属性始终指向创建当前对象的构造函数。比如下面例子:
var arr = [1, 56, 34, 12]; // 等价于 var foo = new Array(1, 56, 34, 12);
document.writeln(arr.constructor === Array);// true
在看下面的例子
var foo = new Function();
var Foo = function() { };
document.writeln(Foo.constructor === Function); // true
// 由构造函数实例化一个obj对象
var obj = new Foo();
document.writeln(obj.constructor === Foo); // true
// 将上面两段代码合起来,就得到下面的结论
document.writeln(obj.constructor.constructor === Function); // true
但是当constructor遇到prototype时,有趣的事情就发生了。
我们知道每个函数都有一个默认的属性prototype,
而这个prototype的constructor默认指向这个函数。如下例所示:
Java代码
function Person(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var p = new Person("ZhangSan");
document.writeln(p.constructor === Person);// true
document.writeln(Person.prototype.constructor === Person); // true
// 将上两行代码合并就得到如下结果
document.writeln(p.constructor.prototype.constructor === Person); // true
function Person(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var p = new Person("ZhangSan");
document.writeln(p.constructor === Person);// true
document.writeln(Person.prototype.constructor === Person); // true
// 将上两行代码合并就得到如下结果
document.writeln(p.constructor.prototype.constructor === Person); // true
当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖),
constructor属性的行为就有点奇怪了,如下示例:
Java代码
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
document.writeln(p.constructor === Person); // false
document.writeln(Person.prototype.constructor === Person); // false
document.writeln(p.constructor.prototype.constructor === Person); // false
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
document.writeln(p.constructor === Person); // false
document.writeln(Person.prototype.constructor === Person); // false
document.writeln(p.constructor.prototype.constructor === Person); // false
为什么呢?
原来是因为覆盖Person.prototype时,等价于进行如下代码操作:
Java代码
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
而constructor属性始终指向创建自身的构造函数,
所以此时Person.prototype.constructor === Object,即是:
Java代码
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
document.writeln(p.constructor === Object); // true
document.writeln(Person.prototype.constructor === Object); // true
document.writeln(p.constructor.prototype.constructor === Object); // true
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
document.writeln(p.constructor === Object); // true
document.writeln(Person.prototype.constructor === Object); // true
document.writeln(p.constructor.prototype.constructor === Object); // true
怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:
Java代码
function Person(name) {
this.name = name;
};
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
Person.prototype.constructor = Person;
// 如此也可以
/*
Person.prototype = new Object({
constructor:Person,
getName: function() {
return this.name;
}
});
Person.prototype = {
constructor:Person,
getName: function() {
return this.name;
}
};
*/
var p = new Person("ZhangSan");
document.writeln(p.constructor === Person); // true
document.writeln(Person.prototype.constructor === Person); // true
document.writeln(p.constructor.prototype.constructor === Person); // true
本文深入探讨了JavaScript中constructor属性的工作原理,解释了它如何指向创建对象的构造函数,并详细分析了在不同情况下,如使用prototype时constructor属性的行为变化及修正方法。
3779

被折叠的 条评论
为什么被折叠?



