Home  >  Article  >  Web Front-end  >  The process of creating objects in the form of new in javascript

The process of creating objects in the form of new in javascript

伊谢尔伦
伊谢尔伦Original
2016-12-10 09:36:101510browse

What exactly does the new operation of JS do? Let's look at it step by step.

For example, I define a function and perform the new operation:

function Foo(){}
var foo = new Foo();

Brain supplement:
Every function will have an attribute called prototype, and the type is object, which is a reference object.
Every object will have an attribute called __proto__. The type field is object, which is also a reference object.

First of all, when the JavaScript engine performs the new operation, it will immediately open up a block of memory and create an empty object (and point this to this object).

Next, execute the constructor function Foo() to construct the empty object (all the attributes and methods in the constructor are installed on the blank object one by one, which is why it is called a constructor).

However, an attribute called __proto__ is added to this empty object, and this __proto__ points to the prototype object of Foo(). In other words, it is __proto__ = prototype;


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn