The 4 stages of object creation using the new keyword

php是最好的语言
Release: 2018-08-09 16:18:40
Original
2788 people have browsed it

Question Analysis

Look at the code first

var Func=function(){ }; var func=new Func ();
Copy after login

new has gone through 4 stages in total

1. Create an empty object
var obj=new Object();
Copy after login
2. Set the prototype chain
obj.__proto__= Func.prototype;
Copy after login
3, let this in Func point to obj, and execute the function body of Func.
var result =Func.call(obj);
Copy after login

4. Determine the return value type of Func:

If it is a value type, return obj. If it is a reference type, an object of this reference type is returned.

if (typeof(result) == "object"){ func=result;}else{ func=obj;;}
Copy after login
Related recommendations:

Use hidden new to create objects

How to use new StdClass() to create in php Empty object?

The above is the detailed content of The 4 stages of object creation using the new keyword. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!