JavaScript Advanced Programming Learning Notes (object-oriented programming) 2

Source: Internet
Author: User

When the constructor is called, a pointer to the original prototype is added to the instance, and we can add properties and methods to the prototype at any time, and can be reflected in the instance, but if the prototype object is re-created, it will cut off the connection of the constructor to the original prototype.

function Dog () {             }    varnew  Dog ();     ={        constructor:dog,        "Bob",        one,        function  () {             alert ("Jump");}    ;    Friend.jump ();     // Error

Here the prototype object is rewritten after the dog instance is created, so friend points to a prototype that does not contain a jump ().

There are many properties that can be shared, such as name, age, in the section where the overrides are made. When we modify the properties of one of the instances, the other instances are affected, which is not what we want. Therefore, the combination of constructors and prototype patterns is often used when creating.

The constructor pattern defines the instance properties, and the prototype schema defines methods and shared properties.

functionDog (name,age,breed) { This. Name =name;  This. Age =Age ;  This. Breed =breed;  This. Friends = ["Ange", "Array"]; } Dog.prototype={constructor:dog, jump:function() {alert ("Jump," please.);    }    }; varDog1 =NewDog ("Bob", "5", "Shiba"); varDOG2 =NewDog ("Amy", "2", "Alaska"); Dog1.friends.push ("Edit");     Console.log (Dog1.friends); //["Ange", "Array", "Edit"]Console.log (Dog2.friends);//["Ange", "Array"]

JavaScript Advanced Programming Learning Notes (object-oriented programming) 2

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.