1 functionBox (name,age) {2 3 This. Name =name;4 5 This. Age =Age ;6 7 This. Family = [' elder brother ', ' sister ', ' sister ';8 9 }Ten OneBox.prototype = { A - Constructor:box, - the Run:functiong () { - return This. name+ This. age+ "in operation" - - } + } - varBox1 =NewBox ("Lee", 100); + A varBox2 =NewBox ("Jack", 200);
Composite Constructors + prototype mode
Using dynamic prototyping mode
1 functionBox (name,age) {2 This. Name =name;3 This. Age =Age ;4 This. Family = [' elder brother ', ' sister ', ' sister ';5 if(typeof This. Run! = "function") {//determine if This.run exists6box.prototype.run=function(){7 return This. name+ This. age+ "in operation"8 } 9 }Ten } One //the initialization of the prototype is as long as it is initialized for the first time, and it is not necessary to initialize each time the constructor is instantiated. A varBox1 =NewBox ("Lee", 100) - varBox2 =NewBox ("Jack", 200)
JS prototype two