JS Object-oriented combination prototype Mode + constructor

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.