1. 程式人生 > >學習EMACScript6新特性

學習EMACScript6新特性

是不是 () fat 實例化 isp function tool 新增 xtend

1、面向對象,新增class、extends關鍵字

在6.0之前3.0 的版本中面向對象編程

//定義類
var Class = function(name,desc){
this.name = name;
this.desc = desc;
this.display = function (){
console.log("Class:{name:"+this.name+",desc:"+this.desc+"}");
}
}
//實例化類一個對象
var a = new Class("Class","定義班級類別");
a.display();//Class:{name:Class,desc:定義班級類別}
var b = new Class("Student","定義學生類別");
//多態
b.other = "父為Class";
//override
b.display = function(){
console.log("Class:{name:"+this.name+",desc:"+this.desc+",other:"+this.other+"}");
}
b.display();//Class:{name:Student,desc:定義學生類別,other:父為Class}

在6.0中面向對象

//定義類Father
class Father{
//Father構造方法
constructor(name,desc){
this.name = name;
this.desc = desc;
}

display(){
console.log("Father:{name:"+this.name+",desc:"+this.desc+"}");
}
}
//定義類Son
class Son extends Father{
//Son構造方法
constructor(name,desc,other){
super(name,desc);
this.other = other;
}
//orverride
display(){
console.log("Son:{name:"+this.name+",desc:"+this.desc+",other:"+this.other+"}");
}
}

var father = new Father("father","父對象father");
var son = new Son("son","子對象son"," 父為father");
father.display();//Father:{name:father,desc:父對象father}
son.display();//Son:{name:son,desc:子對象son,other: 父為father}

感覺是不是跟C#、JAVA十分相近?而且集成、封裝、多態面向對象的三大特性完美支持。

2、Generator 方法 (未完待續)




學習EMACScript6新特性