ECMAScriptjavascript继承之原型式继承(四)

在ECMAScript第55中学,这么些办法获得了封装.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

ECMAScript,上边来探望这一个函数是如何完成持续的.

 

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

 

 

javascript之父DougRuss在2007年付出了那样一串代码,来促成继承.