=波波日志 > JavaScript/Ajax > JavaScript类和继承:constructor属性=
[转]JavaScript类和继承:constructor属性
constructor属性始终指向创建当前对象的构造函数。比如下面例子:
但是当constructor遇到prototype时,有趣的事情就发生了。
我们知道每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数。如下例所示:
当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖),constructor属性的行为就有点奇怪了,如下示例:
为什么呢?
原来是因为覆盖Person.prototype时,等价于进行如下代码操作:
而constructor属性始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object,即是:
怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:
来源:http://developer.51cto.com/art/200907/134913.htm
+展开
-JavaScript
// 等价于 var foo = new Array(1, 56, 34, 12);
var arr = [1, 56, 34, 12];
console.log(arr.constructor === Array); // true
// 等价于 var foo = new Function();
var Foo = function() { };
console.log(Foo.constructor === Function); // true
// 由构造函数实例化一个obj对象
var obj = new Foo();
console.log(obj.constructor === Foo); // true
// 将上面两段代码合起来,就得到下面的结论
console.log(obj.constructor.constructor === Function); // true
var arr = [1, 56, 34, 12];
console.log(arr.constructor === Array); // true
// 等价于 var foo = new Function();
var Foo = function() { };
console.log(Foo.constructor === Function); // true
// 由构造函数实例化一个obj对象
var obj = new Foo();
console.log(obj.constructor === Foo); // true
// 将上面两段代码合起来,就得到下面的结论
console.log(obj.constructor.constructor === Function); // true
但是当constructor遇到prototype时,有趣的事情就发生了。
我们知道每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数。如下例所示:
+展开
-JavaScript
function Person(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
// 将上两行代码合并就得到如下结果
console.log(p.constructor.prototype.constructor === Person); // true
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
// 将上两行代码合并就得到如下结果
console.log(p.constructor.prototype.constructor === Person); // true
当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖),constructor属性的行为就有点奇怪了,如下示例:
+展开
-JavaScript
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // false
console.log(Person.prototype.constructor === Person); // false
console.log(p.constructor.prototype.constructor === Person); // false
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // false
console.log(Person.prototype.constructor === Person); // false
console.log(p.constructor.prototype.constructor === Person); // false
为什么呢?
原来是因为覆盖Person.prototype时,等价于进行如下代码操作:
+展开
-JavaScript
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
getName: function() {
return this.name;
}
});
而constructor属性始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object,即是:
+展开
-JavaScript
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Object); // true
console.log(Person.prototype.constructor === Object); // true
console.log(p.constructor.prototype.constructor === Object); // true
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Object); // true
console.log(Person.prototype.constructor === Object); // true
console.log(p.constructor.prototype.constructor === Object); // true
怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:
+展开
-JavaScript
function Person(name) {
this.name = name;
};
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
Person.prototype.constructor = Person;
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
console.log(p.constructor.prototype.constructor === Person); // true
this.name = name;
};
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
Person.prototype.constructor = Person;
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
console.log(p.constructor.prototype.constructor === Person); // true
来源:http://developer.51cto.com/art/200907/134913.htm
类别:JavaScript/Ajax 作者:转载 日期:2011-08-07 【评论:0】
相关文章
暂时没有评论!
发表留言
热门博文
- AJAX跨域问题解决办法
- ajax问题总结
- jQuery dataType指定为json的问题
- ajax+asp.net+mssql无刷新聊天室
- ajax无刷新上传文件,使用iframe模仿
- ajax对象abort方法
最新博文
- jquery+flash显示图片实时加载进度插件
- jquery浮动层拖动插件
- firefox NPMethod called on non-NPObject wrapped JSObject!错误
- IE浏览器setCapture和releaseCapture介绍
- 51.la统计出问题了
- 隐藏删除ckeditor状态栏
随机博文
- 检查textarea每行的内容是否符合要求
- 万年历的问题实在搞头晕了
- data:image/gif;base64设置图片路径及控件背景
- javascript获取服务端控件值的两种办法
- 求助event.offsetX 兼容IE 和 FF
- Javascript的FSO功能
广告商赞助

