On JavaScript new execution process and function principle

Source: Internet
Author: User

Objective

Recently, learning the essence of JavaScript language, I feel very well written. So here is a summary. How does a method create a process using new, and how does a function statement internally execute it?

In addition to the process of learning, constantly referencing ECMAScript API, can also understand a lot of things. ECMAScript API:W3C Reference Address: http://www.w3school.com.cn/js/pro_js_functions.asp

Body

  1. First, take a look at the output of the example below.

  

  The result is not the same as the result of executing the function directly with the new operator. New returns an object

  2. if the person method itself returns an object . See

  

The result is returned directly from the return, which is the same as the direct execution.

So we can try to come to the conclusion that thenew operator will perform differently depending on the return value of the method.

3. Then,New will change the direction of this, which everyone should know, but let's just say it again.

Executes directly as a function this points to window

  

  This, after using the new operator, points to the current object.

  

So we can also come to the conclusion that: use this with the new operator to point to the current object

4. Next, we say the issue of the person statement, such as:

  

How can a person have so many properties when they're just born? Where did the Applu,bind,call,lenth come from?

  5. with this problem, let's look at an example (another way of declaring a method):

  

You may first ask why you know that function can be used like this? Let's take a look at the description of the show):

  

In fact, the function person () {} specific execution is called the Functions () constructor for construction, the last parameter is the body of the functional.

If he is an instance of function, the person has so many properties/methods, that is, the function object. Look at the function source code, you know!

So you might ask? Oh, no! The properties of function are obviously no more than the person, ah, where are the others? Come and see Zhang.

function in the few of those attributes are actually object properties , the source of the object is not posted out, the front of the ECMAScript API documentation are explained, here also cut a picture (you can take object+ The Function attribute method adds up to see if it is just right).

In fact, function should also be a objecT, so it will also have a series of properties/Methods of object (object and function are inextricably related, can not be expanded to say, too much involved)

Speaking of function (although a bit off the subject, but can help us understand a lot of things). Let's move on to the simulation to implement new.

In fact, what is the operator of new, when it is a method, which is the process of performing the initialization, you can learn the language of JavaScript in the essence of the explanation. The concrete simulation is implemented as follows, all in the comments said (You can only simulate, because you can't see the inside.) :

Don't say a little more to understand ...

Summarize

In fact, there are a lot of points can be extended to lecture, such as _proto_, apply principle, too much, too many, a time also said not finished. Have time to write an article. This article is also for their own summary of deepening memory. There may be some misunderstanding in some places, please point it out in time .

  

  

  

  

On JavaScript new execution process and function principle

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.