zoukankan      html  css  js  c++  java
  • 构造函数创建对象的方式

    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指向那一章
  • 相关阅读:
    LeetCode 55
    LeetCode 337
    LeetCode 287
    LeetCode 274
    LeetCode 278
    LeetCode 264
    LeetCode 189
    LeetCode 206
    LeetCode 142
    LeetCode 88
  • 原文地址:https://www.cnblogs.com/yyy1234/p/15834777.html
Copyright © 2011-2022 走看看