方式1
this.name = n;
this.age = a;
if(this instanceof Person){
alert(‘new调用’);
}else{
alert(‘函数调用’);
}
}
var p = new Person(‘jack’,30); // –> new调用
Person(); // –> 函数调用
方式2
this.name = n;
this.age = a;
if(this instanceof arguments.callee){
alert(‘new调用’);
}else{
alert(‘函数调用’);
}
}
var p = new Person(‘jack’,30); // –> new调用
Person(); // –> 函数调用
方式3
this.name = n;
this.age = a;
if(this.constructor === arguments.callee){
alert(‘new调用’);
}else{
alert(‘函数调用’);
}
}
var p = new Person(‘jack’,30); // –> new调用
Person(); // –> 函数调用
看似很完美,但当把函数/类作为自身实例对象的方法时调用就出问题了
this.name = n;
this.age = a;
if(this.constructor === arguments.callee){
alert(‘new调用’);
}else{
alert(‘函数调用’);
}
}
var p = new Person(‘jack’,30); // 先new一个对象
p.fn = Person; // 把函数/类 Person 赋值给自身对象p的fn属性
p.fn(); // 这句调用时提示“这是new调用”,显然不对
还有更好的方法吗?
—-想了解更多的linux相关异常处理怎么解决关注<计算机技术网(www.ctvol.com)!!>
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/uncategorized/51104.html