function fn(){
console.log(this) //Window 严格模式下指向undefied 非严格模式下指向Window
}
fun()
function fn(){
console.log(this) // fn{}
}
new fn()
1.通过使用new关键词 函数的内部生成一个全新的对象 函数的this指向这个对象
function fn() {
console.log(this)
}
console.log(fn()); // undefined 函数自执行 结果返回undefined
function fn(){
this.x = 10;
}
console.log(new fn()) // fn {x:10}
2.通过new 执行 函数默认返回上述对象
博客介绍了使用new关键词在函数内部生成全新对象,函数的this指向该对象,且通过new执行函数时,默认返回此对象,涉及JavaScript中对象创建的关键知识。
3万+

被折叠的 条评论
为什么被折叠?



