时间:2021-07-01 10:21:17 帮助过:4人阅读
要想实现new的功能,我们首先要知道new是干什么的呢?
在js中 var a = new A() 后
1.创建一个空http://www.gxlcms.com/js-tutorial-378179.html对象obj{}
2.将a的this指向obj{}
3.将a的_proto_指向A的prototype
4.执行A的构造函数
5.返回obj
代码级实现:
function Test (name) {
this.name = name;
this.test = '????';
}
Test.prototype.test2 = function () {
console.log('测试原型链上的方法!');
}
function _new () {
let newObj = {};
let Constructor = Array.prototype.shift.call(arguments);
newObj.__proto__ = Constructor.prototype;
Constructor.apply(newObj,arguments);
return newObj;
}
var t = _new(Test,'yz');
t.test2();相关推荐:
JS核心系列:理解 new 的运行机制
js中的new操作符运行过程是怎样的
以上就是如何实现js中new的功能?js中new的用法的详细内容,更多请关注Gxl网其它相关文章!