The Inheritance in JS

Source: Internet
Author: User

There is a basic pattern for implementing the prototype chain, which is roughly the following code:  function A () {     this.property = true;} A.prototype.getavalue = function () {  return this.property;   };function B () {      This.property = false;} inherited the Ab.prototype = new A (); B.prototype.getbvalue = function () {     return This.property;}; var instance = new B (); Console.log (Instance.getbvalue ());//true;   borrowing constructor  function A () {     this.color = ["Red", "Blue"];}  function B () {     //inherited a     a.call (this);}  var Instance1 = new B (); Instance1.color.push ("yellow"); Console.log (Instance1.color);//red,blue,yellow;  var instance2 = new B (); Console.log (Instance2.color);//red,blue; 1, there is a great advantage to borrowing constructors in relation to the prototype chain, You can pass parameters to a superclass constructor in a subtype constructor, as follows:  function A () {     this.name = name;}  function B () {     //inherits a, passing parameters      a.call (this, "Hello");     // Instance Properties;     this.age = 27;} var instance = new B (); Console.log (instance.name);//helloconsole.log (instance.age);//272. The question of borrowing constructors: if you simply borrow a constructor, you will not be able to avoid the problem with the constructor pattern, which is defined in the constructor, so the reuse of the function cannot be discussed. Moreover, the methods defined in the super-type prototype are not visible to the subtypes, resulting in the fact that all types can only use the constructor pattern. Three. Combination inheritance, sometimes referred to as classic inheritance, refers to the prototype chain and borrowed the technology of the constructor into a piece, so as to play both the length of an inheritance model, the idea is: using the prototype chain to achieve the inheritance of the prototype properties and methods, and by borrowing the constructor to implement the inheritance of the instance properties.  function A (name) {     this.name = name;     this.colors = ["Red", "Blue"];} A.prototype.sayname = function () {     alert (this.name);}; function B (name,age) {     a.call (this,name);     this.age = age;} Inheritance method B.prototype = new A (); B.prototype.constructor = B; B.prototype.sayage = function () {     alert (this.age);}; var Instance1 = new B ("hehe"), Instance1.colors.push ("yellow"); Instance1.sayname ();//heheinstance1.sayage ();//27 ;  var Instance2 = new B ("haha"); Console.log (instance2.colors);//red,blueinstance1.sayname ();// Hahainstance1.sayage ();//28;

Inheritance in

JS

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.