JavaScript Learning (Prototype object)

Source: Internet
Author: User

In JavaScript, the prototype object I think is still more obscure.

In JS we can think of JS is the face of the object, and Java almost, I am also Java origin. The understanding of objects in JS and Java and other strong types of language is not the same, so it will cause a lot of back-end staff learning JS This piece will find it difficult to understand. But JS this prototype flexibility will also cause a lot of problems, here is recommended to read the book "The Best JavaScript language", in addition to the new ES6 introduced the concept of class, in line with the original intention of the book to JS language "take its essence, to its dross."

First, the value type and the reference type are divided into JavaScript. The reference type is an object, and the object is a collection of properties.

The value type is (Undefined, number, String, Boolean), but functions, arrays, objects, NULL, and so on are all objects, so it can be said that in JS in addition to the value type, everything is the object.

The judging value type is typeof, and the reference type is instanceof,instanceof judgment is based on the prototype chain to find the judgment.

Relationship of objects and functions

In summary, objects are created by functions, and functions are objects. Let's check it out.

where Var a={} is equivalent to Var a=new Object ()

Creating an object from a constructor function

We often create objects by constructors when we create them

As you can see, B seems to have a B.A,. b.b attribute, let's judge the result is not that.

Let's take a look at what happens when you create an object from a constructor:

I did four steps.

var b  ={}; b.__proto__ = a.prototype; a.call(b); return b;

It's better understood.

The first line creates an empty object, B.

The second line, which points to the __proto__ member of the empty object to the prototype member object of the constructor object, is the most critical step.

The third row assigns the scope of the constructor to the new object.

Row four returns the new object B. When the constructor contains a return statement, the situation is special, the constructor can contain the return statement (not recommended), but the return value must be this, or other non-object type value.

The second step above involves the object prototype chain Knowledge point,

When we create an object, it says, the object is created by the function. When you create an object, it defaults to the implicit prototype of the newly created object __proto__ to the prototype of the function.

JavaScript Learning (Prototype object)

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.