1. 程式人生 > >ES6模塊化

ES6模塊化

hello exp export pan 書寫方式 UC ora cto turn

前言

語法:import export (註意有無default) 環境:babel編譯ES6語法,模塊化可用webpack 和rollup ES6 Class本身是個語法糖,實際系統默認幫我們轉成JS的構造函數

JS構造函數方式:

class Hello(x,y){
this.x=x;
this.y=y;
}
Hello.protoype.add=function(){
return this.x +this.y;
}
const m =new Hello(2,3);
console.log(m.add()); // 5
typeof Hello === "function" . //true
Hello === Hello.prototype.constructor //true
Hello.__proto__ === Hello.prototype //true

ES6書寫方式:

export class Hello() {
constructor(x,y){
this.x = x;
this.y = y;
}
add() {
return this.x +this.y;
}
}
const m =new Hello(2,3);
console.log(m.add()); // 5
typeof Hello === "function" . //true
Hello === Hello.prototype.constructor //true
Hello.__proto__ === Hello.prototype //true

  

ES6模塊化