原型式继承

function object(o){

function F(){}
F.prototype = o;
return new F();
}

//借助原型可以基于已有的对象创建新的对象,同时还不必因此chu创建自定义类型,

//在object函数的内部,先创建了一个临时性的构造函数,然后将传入的对象作为这个构造函数的原型,最后返回这个临时类型的
//的新实例。从本质上讲,object()对传入其中的对象执行了一次前复制

var person = {
name:"Nicholas",
friends:["shelby","count","wan"]
};

var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");

var anotherPerson2 = object(person);
anotherPerson2.name = "Greg2";
anotherPerson2.friends.push("Rob2");

console.log(anotherPerson2.friends);

 

//为了规范化原型继承 Object.create();

//在传入一个参数的情况下与object函数一样

var person = {
name:"shalio",
friends:["hahs","jk","ll"]
};

var anotherPerson=Object.create(person);

//也可以为此方法传入两个个参数 与Object.defineProperties相似

var newPerson = Object.create(person,{name:{value:"hhhh"}});

posted @ 2014-08-01 09:32  跛脚前行,从心开始  阅读(193)  评论(0编辑  收藏  举报