JavaScript Tutorial 50: Understanding instanceof and prototype chains

Source: Internet
Author: User

1 instanceof:

1.1 Normal use: Determine if an object is an instance of a constructor:

Syntax: The object instanceof function is return true, not return false.

instanceof Object);

1.2 Advanced Usage:

Determines whether the prototype object of a function is on the prototype chain of the instance object

1.3 Cases are as follows:

var arr =instanceof//trueinstanceof Object);  // true

2 prototype chain

The MDN document explains this:

2.1 . JavaScript is often described as a prototype-based language (prototype-based language)-Each object has a prototype object with its prototype as a template, Inherit methods and properties from the prototype.

which

The 1 object invokes the. prototype property, which accesses the prototype object. You can manipulate it.

2 Prototype object called the. Constructor property, accessed to the object.

3 explanation demo is as follows:

(The following example is explained by the constructor person as an object in the document).

functionfunction() {   console.log (1var zs =  new Person (); Zs.sayname (); // 1

4 for the relationship:

With the father ( constructor prototype object ) mother ( constructor ) son ( instance object Zs) to explain the relationship in between.

Interpretation:

Interpretation 1 var zs = new Person ();

Constructor person (mother), created the instance object (son) Zs.

Interpretation 2 Person.prototype.sayName

The person accesses the (father) "prototype object" through Person.prototype and adds the function Sayname.

Interpretation 3 zs.sayname ();//1

(son) Zs can access all the variables and methods within the parent's two objects.

Interpretation 4 Person.prototype.constructor.prototype.sayName

(father) "prototype object" through. Constructor, access to the constructor person (mother).

2.2. Prototype objects may also have prototypes and inherit methods and properties from them * *, one layer at a layer, and so on.

This relationship is often referred to as the prototype chain (prototype chain), which explains why an object has properties and methods that are defined in other objects.

The relationship looks like this:

explanation :

2.1 Prototype object (father) itself, which could be created also as a son. That is: The prototype object (father) also has its father mother, to inherit their attribute method to use.

2.2 Through this layer of relationships, from the * * Instance object ~ Null * * constitutes a prototype chain.

3. Prototype chain use:

3.1 Based on the rules that are unique to the prototype chain, we usually add public attributes within the object ; Add a private property inside the prototype object .

4 Rules for object lookup properties:

First find the property on their own, if there is, directly return, if not, follow the prototype chain to look up

JavaScript Tutorial 50: Understanding instanceof and prototype chains

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.