Js new的本质
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);
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
