function myNew(constructor) {
var obj = {
__proto__: constructor.prototype
};
constructor.apply(obj, Array.prototype.slice.call(arguments, 1));
return obj;
}
function MyTest(i) {
this.count = i;
}
var obj1 = myNew(MyTest, 0);
console.log(obj1.count);
console.log(obj1 instanceof MyTest);
console.log(obj1 instanceof Object);
function OtherTest(i, name, value) {
this.count = i;
this.name = name;
this.value = value;
}
var obj2 = myNew(OtherTest, 0, 'myName', 'myValue');
console.log(obj2);
console.log(obj2 instanceof MyTest);
console.log(obj2 instanceof OtherTest);
A way of understanding the 'new' operator
最新推荐文章于 2021-04-26 10:49:21 发布