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 方法 (未完待续)
原文地址:http://blog.51cto.com/13584768/2066475
时间: 2024-10-08 10:41:41