Prototype inheritance in JavaScript

Source: Internet
Author: User

The basic rules that JavaScript circles become:

    1. All data are objects;
    2. To get an object, instead of instantiating the class, find an object as a prototype and clone it;
    3. The object will remember its prototype;
    4. If the object cannot respond to a request, it delegates the request to its own prototype;
    • All the data are objects
    1. The types of JavaScript are divided into two categories: basic types and complex types;

The basic types are: Undefined, number, Boolean, String, NULL, and the base type can be processed by wrapping the class into object type data;

Complex Type: object;

1.1 Packaging object: The so-called "packaging object", which is corresponding to the value, String, Boolean, and Number String Boolean three native objects. These three native objects can turn the values of the original type into (wrapped) objects.

1.2 The correspondence between the wrapper class and the basic type:

Basic type Wrapping Object
String String
Number Number
Boolean Boolean
         Varv1 = new number(123);
var v2 = new String ("abc");
var v3 = New Boolean (TRUE);

typeof V1;//object
typeof V2;//object
typeof v3;//object

V1 = = 123;//fal Se
v2 = = = "abc";//false
V3 ===true;//false
1.3 number , string and boolean if not called as a constructor (that is, the call is not added Code class= "Highlighter-rouge" >new ), often used to convert any type of value to numeric, string, and Boolean values.
1.3.1 Boolean function type conversion
Boolean (undefined),//false
Boolean (null),//false
Boolean (0),//f Alse
Boolean ("");//false
Boolean (NAN);//false

Boolean (1);//true
Boolean (' false ' ');//true
Boolean ([]);//true
Boolean ({});//true
Booleam (function () {});//true
Boole An (/foo/);//true

Using the double no operator ( ! ) can also convert any value to a corresponding Boolean value.

        !! Undefined//false

!! Null//false

!! 0//false

! "'//false

!! NAN//false

!! 1//true

!!‘ False '//false

!! []//true

!! {}//true

!! function () {}//true

!! /foo///true

       Finally, for some special values, the Boolean object is preceded by an addition new , which results in the exact opposite and must be carefully

The root object in 1.4 JavaScript is the Object.prototype object;

Object.prototype is an empty object, and every object we encounter is cloned from a Object.prototype object;

        var obj1 = new Object ();

var obj2 = {};

Console.log (object.getprototypeof (obj1) = = = Object.prototype); True

Console.log (object.getprototypeof (obj2) = = = Object.prototype);//true

ECMASCRIPT5 provides the object.getprototypeof to view the object's prototype;

    • To get an object, instead of instantiating the class, find an object as a prototype and clone it

2.1 In the JavaScript language, we do not care about the details of cloning, because it is implemented inside the engine. All we need is a display call to var obj = new Object () or var obj2 = {}. At this point, the engine internally clones an object from the Object.prototype, and we finally get the object.

function person (name) {

THIS.name = name;

};

Person.prototype.getName = function () {

return this.name;

};

    

var a = new person ("seven");

Console.log (a.name);//seven

Console.log (a.getname);//seven

Console.log (object.getprototypeof (a) = = = Person.prototype);//true

Supplementary explanation: The person is not a class, but a constructor, and JavaScript functions can be called either as normal functions or as constructors. When the function was called with the new operator, the function at that time was a constructor. The process of creating an object with the new operator is actually just a process of cloning the Object.prototype object first and then doing some additional operations.

2.2 _proto_ Properties

2.2.1 Under the browser that exposes _proto_ properties such as Chrome and Firefox, we can use this code to understand the process of the new operation.

function person (name) {

THIS.name = name;

};

Person.prototype.getName = function () {

return this.name;

}

    

var objectfactory-= function () {

var obj = new Object (); Cloning an empty object from the Object.prototype

var Constructor = [].shift.call (arguments);//call means to put an empty array method on the arguments to execute, originally arguments is not the shift () method, now is to put an empty array of shift ( ) method is placed on arguments to execute, so argument the value of the original first element (person).

Obj._proto_ = Constructor.prototype; Obj._proto_ = Person.prototype;

var ret =constructor.apply (obj, arguments)

Function.apply (Obj,args) method can receive two parameters

OBJ: This object will replace the This object in the function class (This object inherits the properties and methods in the function)

Args: This is an array, which is passed as a parameter to function (args-->arguments)

return typeoof ret = = = ' object '? Ret:obj;

}

var a = objectfactory (person, ' seven ');

Console.log (a.name);//seven

Console.log (A.getname ());//seven

Console.log (object.getprototypeof (a) = = = Perso.prototype); True

    • The object remembers its prototype.

The prototype chain lookup mechanism in 3.1Javascript language, each object remembers its own prototype

3.2 "Prototype of the object", in terms of JavaScript's true implementation, does not say that the object has a prototype, but only to say that the object's constructor has a prototype, for the "object to the request entrusted to its prototype" this sentence, the better is that the object has delegated the request to his constructor prototype.

3.3 JavaScript provides an object with a hidden property called _proto_, and the _proto_ property of an object touches the prototype object of its constructor by default, which is {constructor}.prototype, in some browsers, _proto_ Be published, use this code on Chrome or Firefix to verify

var a = new Object ();

Console.log (a._proto_ = = = Object.prototype);//true

in fact, _proto_ is the object and the prototype of the object constructor. "The bond of connection.

    • If the object cannot respond to a request, he will delegate the request to his constructor.

The person prototype object is also an object, and when the object cannot find a property, It is found in the object prototype , which is the prototype chain

  

    


 
  
 

For other things about objects, follow up on subsequent articles

Prototype inheritance in JavaScript

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.