瞌睡中的葡萄虎

博客园 首页 新随笔 联系 订阅 管理

为了解决原型所带来的问题,此处需要通过组合构造方法和原型来实现对象的创建,将属性在构造方法中定义,将方法在原型中定义。这种有效集合了两者的优点,是目前最为常用的一种方式。

function Person(name,age,friends){
    //属性在构造函数中定义
    this.name = name;
    this.age = age;
    this.friends = friends;
}
Person.prototype = {
    constructor:Person,
    //方法在原型中定义
    say:function() {
        alert(this.name+"["+this.friends+"]");
    }
}
//此时所以的属性都是保存在自己的空间中的
var p1 = new Person("Leon",23,["Ada","Chris"]);
p1.name = "John";
p1.friends.push("Mike");//为p1增加了一个朋友
p1.say(); //John  ["Ada","Chris","Mike"]
var p2 = new Person("Ada",33,["Leon"]);
p2.say();//Ada Leon

内存模型图如下所示:

 

posted on 2014-09-03 18:06  瞌睡中的葡萄虎  阅读(220)  评论(0编辑  收藏  举报