其他分享
首页 > 其他分享> > 构造函数创建对象的方式

构造函数创建对象的方式

作者:互联网

function User(name){
    this.name=name;
    this.show=function(){
        console.log(this);
    }
}

let xj=new User("xj");
xj.show();//对象去调用的时候this指向当前对象     User {name: 'xj', show: ƒ}

//如果是当作普通函数调用,this指向是window
let xj1=xj.show;
xj1();//Window {window: Window, self: Window, document: document, name: '', location: Location, …}
// 严格模式下,this是undefined
//可以看this指向那一章

 

标签:xj,name,方式,show,创建对象,Window,User,指向,构造函数
来源: https://www.cnblogs.com/yyy1234/p/15834777.html