码迷,mamicode.com
首页 > Web开发 > 详细

JS创建对象模式及其对象原型链探究(五):组合使用构造函数模式和原型模式

时间:2016-03-26 07:18:52      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

组合使用构造函数模式和原型模式

构造函数模式用于定义实例属性,原型模式用于定义方法和共享的属性。

创建自定义类型的最常见方式,就是组合使用构造函数模式和原型模式。

1.创建对象

// 组合使用构造函数模式和原型模式
function Person(name, age, job){
     this.name = name;
     this.age = age;
     this.job = job;
     this.friend = ["Jenny", "Court"];
}

Person.prototype = {
     constructor: Person,       //constructor指向Object构造函数,这里要将其constructor恢复为指向Person构造函数。
     Country: "China",
     showName: function(){
          console.log("name = " + this.name);
     }
}

var p1 = new Person("Mike", 20, "student");
var p2 = new Person("Tom", 23, "Teacher");

console.log(p1);
console.log(p2);

技术分享

结果,每个实例都会有自己的一份实例属性的副本,但同时又共享着对方法的引用,最大限度地节省了内存。

另外,这种混合模式,还支持向构造函数传递参数。

2.不同之处

看上面的代码,你会发现有个不同之处:Person.prototype的constructor属性默认是指向Object构造函数。

从何而知?我们来测试一番。

// 组合使用构造函数模式和原型模式
function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.friend = ["Jenny", "Court"];
}
// 未使用原型模式前,Person构造函数的原型对象的constructor指向Person构造函数本身
console.log("Person.prototype.constructor === Person:");
console.log(Person.prototype.constructor === Person);
console.log("-----分割线-----");

Person.prototype = {
    // constructor: Person,     //一般要认为设置,将其constructor恢复为指向Person构造函数,这里为了演示,就没设置。
    Country: "China",
    showName: function(){
        console.log("name = " + this.name);
    }
}

// 使用原型模式,我们将Person.prototype设置为等于一个以对象字面量形式创建的新对象
// 因此,这里constructor属性也就变成了新对象的constructor属性,指向Object构造函数。
// Person构造函数的原型对象的constructor指向Object构造函数
console.log("Person.prototype =");
console.log(Person.prototype);
console.log("-----分割线-----");

console.log("Person.prototype.constructor === Object:");
console.log(Person.prototype.constructor === Object);
console.log("-----分割线-----");

console.log("Person.prototype.__proto__ === Object.prototype:");
console.log(Person.prototype.__proto__ === Object.prototype);

var p1 = new Person("Mike", 20, "student");
var p2 = new Person("Tom", 23, "Teacher");

console.log(p1);
console.log(p2);

技术分享

技术分享

未使用原型模式前,Person构造函数的原型对象的constructor指向Person构造函数本身。
使用原型模式,我们将Person.prototype设置为等于一个以对象字面量形式创建的新对象。
因此,这里constructor属性也就变成了新对象的constructor属性,指向Object构造函数。
所以,Person构造函数的原型对象的constructor指向Object构造函数。

3.观察Person构造函数涉及到的原型链

console.log("Person.prototype=");
console.log(Person.prototype);
console.log("-----分割线-----");

console.log("Person.prototype.constructor === Person:");    //true
console.log(Person.prototype.constructor === Person);    //true
console.log("-----分割线-----");

console.log("Person.prototype.__proto__ === Object.prototype:");    //true
console.log(Person.prototype.__proto__ === Object.prototype);    //true
console.log("-----分割线-----");

console.log("Person.__proto__=");
console.log(Person.__proto__);
console.log("Person.__proto__ === Function.prototype:");
console.log(Person.__proto__ === Function.prototype);    //true
console.log("-----分割线-----");

技术分享

4.观察p1实例对象涉及到的原型链

console.log("p1.prototype=");
console.log(p1.prototype);
console.log("-----分割线-----");

console.log("p1.__proto__=");
console.log(p1.__proto__);
console.log("p1.__proto__ === Person.prototype:");
console.log(p1.__proto__ === Person.prototype);        //true
console.log("-----分割线-----");

console.log("p1.__proto__.constructor === Person:");
console.log(p1.__proto__.constructor === Person);
console.log("-----分割线-----");

console.log("p1.__proto__.__proto__=");
console.log(p1.__proto__.__proto__);
console.log("p1.__proto__.__proto__ === Object.prototype:");
console.log(p1.__proto__.__proto__ === Object.prototype);    //true
console.log("-----分割线-----");

技术分享

5.观察下p1.showName属性引用的函数

console.log("p1.showName.prototype=");
console.log(p1.showName.prototype);
console.log("-----分割线-----");

console.log("p1.showName.prototype.constructor=");
console.log(p1.showName.prototype.constructor);
console.log("-----分割线-----");

console.log("p1.showName.prototype.__proto__=");
console.log(p1.showName.prototype.__proto__);

console.log("p1.showName.prototype.__proto__ === Object.prototype:");    
console.log(p1.showName.prototype.__proto__ === Object.prototype);    //true
console.log("-----分割线-----");

console.log("p1.showName.__proto__=");
console.log(p1.showName.__proto__);
console.log("p1.showName.__proto__ === Function.prototype:");    
console.log(p1.showName.__proto__ === Function.prototype);    //true
console.log("-----分割线-----");

技术分享

原型链图

技术分享

JS创建对象模式及其对象原型链探究(五):组合使用构造函数模式和原型模式

标签:

原文地址:http://blog.csdn.net/monkey_dcj/article/details/50983882

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!