//方法一
function Person(n,a){
this.name = n;
this.age = a;
if(this instanceof Person){
console.log("是被new调用")
}else{
console.log("函数调用")
}
}
var p = new Person('axin',30)
Person()
//方法二
function Person1(n,a){
this.name = n;
this.age = a;
if(this instanceof arguments.callee){
//arguments.callee它可以用于引用该函数的函数体内当前正在执行的函数。这在函数的名称是未知时很有用,例如在没有名称的函数表达式 (也称为“匿名函数”)
console.log("被new调用")
}else{
console.log("函数调用")
}
}
var p1 = new Person("zy",26)
Person1()
判断是否被new调用
最新推荐文章于 2023-08-03 10:20:10 发布