Home  >  Article  >  Web Front-end  >  The 4 stages of object creation using the new keyword

The 4 stages of object creation using the new keyword

php是最好的语言
php是最好的语言Original
2018-08-09 16:18:402896browse

Question Analysis

Look at the code first

var Func=function(){
};
var func=new Func ();

new has gone through 4 stages in total

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

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;;}
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!

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