Lab Notes on Constructors and prototypes

Source: Internet
Author: User

For constructor, I think of an example. B was born of B, but B cut off his mother-child relationship. A had no children and decided to adopt B. So A became B's stepmother, that is, B's legal mother. If you ask B's mother what is called, it is equivalent to asking B who is the constructor. This relationship can easily be changed. Just set up a document or proof. The prototype is equivalent to B's genes, which cannot be changed due to adoption. Unless all its genes have been changed, then it is natural that B is its mother-in-law.

// School function School (name) {this. name = name; this. teachers = []; this. level = function () {console. log ('university ') }}// attribute School added to the prototype. prototype. students = []; // School function -- introduces School. prototype. show = function () {console. log (this. name + ': Great school! ') This. teachers. forEach (function (teach) {console. log (this. name + '-' + teach);}) // School function --- recruitment Instructor School. prototype. recruit = function (name) {this. teachers. push (name);} // School function --- enrollment School. prototype. enroll = function (name) {this. students. push (name) ;}// create the object var ts = new School ('Tsinghua University ') by using the constructor '); // at this time, School is the ts constructor var bj = new School ('beijin University '); // at this time, School is the object created by the constructor of bj, note two questions // 1 The properties and methods in the constructor are not shared. // 2 the properties and methods on the prototype are shared. ts. recruit ('frog'); // frog is the teacher bj of ts. recruit ('Aaron '); // Aaron is the teacher of bj's school ts. show (); // frog only appears in ts bj. show (); // Aaron only appears in bj. // The attributes declared in the constructor are not shared with the console. log (bj. teachers = ts. teachers); // false // The methods declared in the constructor are not shared with the console. log (bj. level = ts. level); // false; // it indicates that teachers is correct in the constructor. teachers in different schools do not affect ts. enroll ('Tom '); // only provides the ts school enrollment console. log (bj. students) // ts school does not enroll students This student also exists bj. enroll ('jek'); console. log (ts. students) // for students recruited by bj, the prototype is also available in ts. // The prototype method is shared console. log (bj. enroll = ts. enroll) // true // indicates that the prototype property is a shared console. log (bj. students = ts. students) // true; // Summary: the method should be written on the prototype to save resources, and the attribute of the reference type should be written in the constructor to avoid pollution; // Constructor question function A () {this. name = 'a';} function B () {this. name = 'B';} var a = new A; console. log (. constructor = A) // true // replace B's prototype object with A's instance object B. prototype = New A; var B = new B; console. log (B. constructor = B) // false console. log (B. constructor = A) // true // indicates that rewriting the prototype object will result in A problem pointed to by the constructor. // The constructor is modified to B. prototype. constructor = B; console. log (B. constructor = B) // true // constructor is mounted on prototype. If prototype is rewritten, the constructor console is automatically rewritten. log (B. isPrototypeOf (B) // false; console. log (Object. getPrototypeOf (B) // A // The contructor cannot change the prototype reference relationship either. // This method also produces the B constructor pointing to A question. Question B. prototype = {//...} // It is recommended to write. prototype. getName = function () {console. log (this. name);} function C () {this. name = 'C';} C. prototype. getAge = function () {console. log ('C age 18')} var c = new c; // only change the Constructor C. prototype. constructor = A; console. log (Object. getPrototypeOf (c) // A console. log (c. isPrototypeOf (C) // falseconsole. log (c. isPrototypeOf (A) // falseconsole. log (c) // C; // c. getName (); // u Ndefinedc. getAge (); // c age 18 // conclusion: as long as the object prototype is changed, its function creation automatically changes. // only changes the point of the constructor, the prototype is inaccurate but not rewritten.

Lab Notes on Constructors and prototypes

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.