Example: var obj = new Array () ; This step made a total of three things: the obj = {} var; obj. Proto = Array.prototype; Array.call (obj); a first line, we have created a null object obj second line, we will __proto__ members of this empty object points to the third row of function objects Array prototype object is a member, we will replace obj Array this pointer function object. Sometimes manually write a function NEW
` var New =function(func, name, age) {//手动创建一个NEW函数并模拟new对象过程
var res = {};//创建一个新对象
if (func.prototype != null) {//情况判断,避免函数没有原型的情况
res.__proto__ = func.prototype;//让这个创建的新对象指向构造函数的原型,
//因为调用原型上的函数,属性等是使用__proto__,而非prototype;
}
var ret = func.apply(res, Array.prototype.slice.call(arguments, 1)); //通过借用函数并执行
//构造函数返回构造函数返回值
if ((typeof ret === 'object' || typeof ret === 'function') && ret != null) {
return ret;//返回前判断下类型,分类出不返回值的构造函数和有返回值的构造函数
}
return res;//返回创建的这个新对象
}
var func=function(a,b){
this.a = a;
this.b = b;
}
var newfunc = New(func,1,2)
`