Problems with JavaScript's Zhongyuan chain

Source: Internet
Author: User

We know that using a prototype chain to implement inheritance is a goodway:) See example of a prototype chain inheritance.

function A () {   this. ABC =thefunction  () {  return this . ABC;}; function  New A ();          // B Through an example of a to complete the inheritance, forming a prototype chain (B's prototype is a example) var New B (); B.getabc ();

The relationship is as follows: B (instance)->b.prototype = new A (), A.prototype->object.prototype

However, there are problems in this seemingly "beautiful" approach to inheritance.

1. The main problem comes from prototypes that contain reference type values, and we know there is a problem with shared prototypes, and an example is thrown

function Person () {                 = {    = ["A", "B"];  } var New Person (); var New Person ();p Erson1.friends.push ("C"); Console.log (person1.friends);   // "A", "B", "C"Console.log (person2.friends);  // "A", "B", "C"

By referencing an instance, the value in the original in the prototype is changed and other instances are affected as well. (This is why reference type values are defined in the constructor, not in the prototype)

The same thing happens in the prototype chain:

functionA () { This. numbers = [All];}functionB () {}b.prototype=NewA ();varb =NewB ();varA =NewA (); B.numbers.push (4);             B.numbers; //1234varB2 =NewB (); b2.numbers; //1234a.numbers; //123

We see the same situation as above (when inheriting from a prototype, the prototype will actually become an instance of another type.) As a result, the original instance attribute becomes the prototype attribute now.

But we see an example of a a.numbers; is still 123, stating that when B inherits an instance of a, it replicates all of the attributes in the a instance (including the prototype pointer, forming the prototype chain) not a reference (in fact, it is because the inheritance is an instance of a () so it does not affect the performance of a () to create other instances? )。

2. When you create a subclass instance, you cannot pass parameters to the superclass without affecting all object instances.

  function   A (light) {        the. light1 = Light ;    };    function (Light   ) {        the. Light = light;      };     // Assigning a value to B at the same time, want to assign a value, can not achieve    New A ();     var New B (123);    Console.log (c.light);    Console.log (c.light1);

To implement this need to manually invoke A's constructor, it affects other instances

 function   A (light) { this . light1 = light; };  function   B (light) { this . Light = light; A.call ( this , Light); //     };  //  assigns a value to B while assigning a value to a  B.prototype = Span style= "color: #0000ff;"    >new   A ();  var  C = new  B (123);    Console.log (C.light); Console.log (c.light1);  

Problems with JavaScript's Zhongyuan chain

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.