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

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

伊谢尔伦
Release: 2016-12-10 09:36:10
Original
1602 people have browsed it

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();
Copy after login

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;


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template