天天看點

new關鍵字的執行過程

new的執行過程
     1、在記憶體中建立了一個空的對象
     2、讓構造函數中的this指向剛剛建立的對象
     3、執行構造數,在構造函數中設定屬性和方法
     4、傳回了目前對象函
           
//Student自定義構造函數
        function Student(name,age){
            console.log(this);
        //屬性
            this.age=age;
            this.name=name;
        //方法
            this.sayHi=function(){
                console.log(this.name+':hello');
            }
            console.log(this);
        }
        
        var stu1=new Student('lisi',13);
        console.log(stu1);
        stu1.sayHi();
        

           

繼續閱讀