//代码开始
function Person(name) {
this.name = name;
console.log(this.name); //Output 1
console.log(this); //Output 2
}
var p1 = new Person("Object_Shashank");
var p2 = Person("Function_Shashank");
//代码结束
p1:
p2:
有人可以解释" p2:输出2"
答案 0 :(得分:1)
它打印window
对象,因为this
引用了窗口对象。
function Person(name){
this.name=name;
console.log(this.name); //Output 1
console.log(this); //Output 2 <-- this `this` will point to the object it belongs to , which in this case of p1 is Object_Shashank while for p2 is window
}
var p1=new Person("Object_Shashank");
var p2=Person("Function_Shashank"); // Equivalent to p2 = window.Person("Function_Shashank")
编辑。添加了代码示例