js中class類的基本理解及相關知識(一)


// ES6的class類
        class Point{
            constructor(){

            }

            fn1(num){
                console.log("被調用了")
            }
        }
        console.log(typeof Point);//function
        console.log(Point===Point.prototype.constructor);//true

        // 調用實例上的方法其實就是調用原型上的方法
        Point.prototype.fn1()//被調用了
        var b=new Point();
        b.fn1();//被調用了
        console.log(b.constructor===Point.prototype.constructor);//true

        Point.prototype.constructor.fn1()//報錯
        Point.fn1()//報錯
// 給類添加新方法(這些方法都會加在原型prototype上)
        class Point{
            constructor(){

            }
        }
        Object.assign(Point.prototype,{
            fn1(){},
            fn2(){},
        })
        var b=new Point();
        console.log(b);
打印結果為:
 
         
        class Point{
            constructor(){

            }
        }
        Object.assign(Point.prototype,{
            fn1(){},
            fn2(){},
        })
        console.log(Point.prototype);
  打印結果為:
  

 

        class Point{
            constructor(){

            }
        }
        var b=new Point();
        Object.assign(b,{
            fn1(){},
            fn2(){},
        })
        console.log(b);
  打印結果為:
  

 

 

 

 
         

 

 
         

 

 
         

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM