JavaScript object-Oriented programming record Note 4

Source: Internet
Author: User

Combine the constructor pattern with the prototype pattern:

  

function person (name, age, Job) {  this.name = name;  This.age = age;  This.job = job;  This.friends = [' Shelby ', ' Court '];    } Person.prototype = {  Connstructor:person,  sayname:function () {    return this.name;  }    } var Person1 = new Person (' Tom ', ' software engineer '); var person2 = new Person (' Jack ', ' Doctor '); Person1.friend.push (' Var '); Console.log (person1.friend); Output ' Shelby ', ' Court ', ' Var ' Console.log (person2.friend);  output ' Shelby ', ' Court ', console.log (person1.friend = = = Person2.friend);//Falseconsole.log (person1.sayname = = = Person2.sayname); True

Because the Sayname method belongs to the prototype property, Person1.sayname is identical to person2.sayname and references the same address.

Dynamic Prototyping Mode

function person (name, age) {    this.name = name;    This.age = age;    if (typeof this.sayname = = = ' Fucntion ') {     Person.prototype.sayName = function () {         alert (this.name);    }   }    //When creating the person instance object for the first time, because the Syaname method does not currently exist, the syntax block in if is executed, the Sayname method is placed on the shared object prototype, and when the person object is later instantiated, Because the Sayname property already exists, it is not created.

Parasitic constructors: (parasitic constructors are similar to factory patterns, except for a new one)

function person (name, age) {    var o = new Object ();    O.name = name;    O.age = age;    O.sayname = function () {        alert (this.name);    }    return o;}

  

Secure constructor Mode

Secure Object : The so-called prudent object, refers to the absence of public properties, and other methods do not reference the This object. A secure object is best for use in some secure environment, or to prevent data from being altered by other applications.

JavaScript object-Oriented programming record Note 4

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.