javascript继承的原型式继承(四)

javascript之父道格拉斯当2006年让来了这般平等差代码,来促成继承.

1 function object(o) {
2             function F() { }
3             F.prototype = o;
4             return new F();
5         }

下面来看望这函数是怎么样兑现连续的.

 1 var Human = {
 2             name: "guo",
 3             colors: ["red","black"]
 4         }
 5         var one = object(Human);
 6         one.colors.push("yellow");
 7 
 8         var two = object(Human);
 9         two.colors.push("green");
10 
11         alert(two.colors); //red,black,yellow,green

当ECMAScript5被,这个措施取得了封装.Object.create();

1 var Human = {
2             name: "guo",
3             colors: ["red", "black"]
4         }
5         var one = Object.create(Human);
6         one.colors.push("yellow");
7         var two = Object.create(Human);
8         two.colors.push("green");
9         alert(two.colors);// red,black,yellow,green