您的位置:首页 > Web前端 > JavaScript

Js new的本质

2016-03-11 14:55 435 查看
new的本质

如果new运算符是一个方法而不是一个运算符.

Function.prototype.new = function(){

// 创建一个新对象that,新对象继承自构造函数

var that = Object.create(this.prototype);

// 用新函数的指针that,调用构造函数,并得到返回值other

var other = this.apply(that,arguments);

// 判断构造函数中,return的是对象,是则返回,不是则返回新对象that

return (typeof other === ‘object’ && other || that);

}

var child = Parent.new(arguments); –> child = new Parent(arguments);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: