JS中的constructor
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JS中的constructor,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3002字,纯文字阅读大概需要5分钟。
内容图文
// constructor属性始终指向创建当前对象的构造函数。比如下面例子:比如下面例子:
//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 Foox = new Foo();
console.log(Foox.constructor === Foo); // true
//将上面两段代码合起来,就得到下面的结论
console.log(Foox.constructor.constructor === Function); // true
//但是当constructor遇到prototype时,有趣的事情就发生了。 我们知道每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数。如下例所示:
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
//当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖),constructor属性的行为就有点奇怪了,如下示例:
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
//为什么呢? 原来是因为覆盖Person.prototype时,等价于进行如下代码操作:
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
//而constructor属性始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object,即是:
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
//此处的Object即为下面的Object
//Person.prototype = new Object({
// getName: function() {
// return this.name;
// }
//});
//怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:
function Person(name) {
console.log(this); //Person(ZhangSan)
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
// 顺带说说继承,以上的称为原型继承,下面的继承只能继承私有属性
// function Father(name){..this..}
// Father()之中的this在编译阶段不会赋值,最后会指向new Father()对象
function Father(name){
this.name = "LiuYashion";
this.age = 23;
};
Father.prototype.getName = function(){
console.log(this) // 这里的this指向的是new Father(),即temp2
console.log(this.name);
};
function Son(){
//Father.call(this,["name","age"]);
Father.apply(this,["name","age"]);
//call跟apply的区别只在于后面参数的传法,不赘述
};
var temp1 = new Son();
console.log(temp1.name+" "+temp1.age); //LiuYashion 23
var temp2 = new Father();
temp2.getName(); //LiuYashion
原文:http://www.cnblogs.com/nemoro1928/p/5388013.html
内容总结
以上是互联网集市为您收集整理的JS中的constructor全部内容,希望文章能够帮你解决JS中的constructor所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。