we all know that the greatest advantage of prototype model building objects is sharing, but you know, the biggest drawback of prototype mode is its greatest advantage, if the sharing is the method of the general will not have any effect on our expectations, but if the other reference types are involved in the trouble, see as follows;
//disadvantages of the prototypes functionBox () {} Box.prototype={constructor:box, Name:' Lee ', Age:20, family:[' elder brother ', ' sister ', ' sister '] } varBox1 =NewBox (); varBox2 =NewBox (); alert (box1.family);//brother, sister, sisterBox1.family.push (' brother ')); After the first instance of the modified reference type, the shared alert (box1.family) is maintained;//brother, sister, sister, brotheralert (box2.family)//brother, sister, sister, brother//shared the box1 after the added reference type
To solve this, we can use the combined prototype pattern + constructor
JS Object-oriented combination prototype Mode + constructor