About implementing inheritance in JavaScript, and prototype properties

Source: Internet
Author: User

Thanks to Mozilla for letting me understand inheritance.

There are eight basic types of JavaScript, and the function belongs to object. So all functions are inherited from object. Extensions: objects, basically everything in JavaScript is an object, and can be stored in variables. Keep this in mind.

But JavaScript does not have a real sense of inheritance. Instead, it is similar to pointing and copying. And each function and object has a prototype property. ("Prototype Chain" (prototype chain) mode)

All are constructed as a tree-like structure of the prototype chain.

Several common inheritance methods are:

1.

functionClassA (scolor) {this.color = Scolor; This.saycolor = function () {alert ( this . Color); };}functionClassB (Scolor, sName) {This.newmethod = ClassA;    This.newmethod (Scolor);    Delete This.newmethod;  This. Name =SName;  This. Sayname =function() {alert ( This. Name); };}varObja =NewClassA ("Blue");varOBJB =NewClassB ("Red", "John"); Obja.saycolor (); //output "Blue"Objb.saycolor ();//output "Red"Objb.sayname ();//output "John"

The above code, the main in the Red section, first explained the CLASSB function body Red first sentence:

This.newmethod = ClassA;
this refers to the owner of the current object . Code, This.newmethod = ClassA in the function body of CLASSB.
If I var v=new ClassB (); This in the//CLASSB function body refers to V, because V is the owner of the ClassB function body.
The above code, specifically through what steps, we first analyze:
This must refer to the CLASSB. So this.newmethod can be interpreted as CLASSB create a new Newmethod property and assign a value of ClassA. This. XXX quickly create a property, or modify a property value

Then explain the second sentence of CLASSB red:
This.newmethod (Scolor);
The key in this second sentence, the code of this is undoubtedly referring to the CLASSB. Concrete execution means: Execute the Newmethod function. is to execute classa.
Step explanation
Newmethod since the assignment is ClassA, then Newmethod has ClassA all the properties and methods
function Newmethod (scolor) {    this. color = scolor;       This function () {        alert (this. color);}    ;}

Previously said, this is thecurrent object owner , Newmethod is the property of the CLASSB, all Newmethod function body of this refers to the CLASSB

The code in the body of the execution function is, classb.color=scolor; classb.saycolor=function...;//Adding properties dynamically

The third sentence in CLASSB:

Delete this.newmethod;//deleting properties
Since I classb already have the method and the attribute in the ClassA, Newmethod has no use to CLASSB.

2.

functionClassA () { This. color = "Red";  This. Saycolor =function() {Console.log ( This. color); };} ClassA.prototype.height=100;functionClassB () { This. color= "Bule";} Classb.prototype=NewClassA (); ClassB.prototype.width=200;varb =NewClassB (); B.saycolor ();//Bule

The following example:

 varBasecalculator =function() {         This. t=2; };//This is a constructor functionBasecalculator.prototype={add:function(x, y) {returnX +y; }, Subtract:function(x, y) {returnX-y;    }    }; varCalculator =function () {        //declare a number for each instance        This. Tax = 5;    }; Calculator.prototype=NewBasecalculator ();//Inheritance   //Calculator.prototype.constructor=calculator;Calculator.prototype.add =function(x, y) {returnX + y + This. Tax; };

(The style drawing is based on the code above)

Like a style map--detailed explanation

About implementing inheritance in JavaScript, and prototype properties

Related Article

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.