ES6的class方法基本用法
2016-11-28 00:00
225 查看
摘要: ES6的class方法基本用法
在ES5中我们通常通过构造函数,定义并生成新对象。
例如:
而在ES6中引入了class的概念,ES6的class可以看成一个语法糖(语法糖:指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会。)
现在我们可以用ES6的class这样定义一个Point,
在ES5中我们通常通过构造函数,定义并生成新对象。
例如:
function Point(name,age){ this.name=name; this.age=age; } Point.prototype={ Who:function(){ return "My name is "+this.name+",My age is "+this.age; } } var p=new Point("ND",25); console.log(p.Who()) //My name is ND,My age is 25
而在ES6中引入了class的概念,ES6的class可以看成一个语法糖(语法糖:指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会。)
现在我们可以用ES6的class这样定义一个Point,
class Point{ constructor(name,age){ this.name=name; this.age=age; } Who(){ return "My name is "+this.name+",My age is "+this.age; } } var p=new Point("ND",25); console.log(p.Who()) //My name is ND,My age is 25
相关文章推荐
- ES6 类(Class)基本用法和静态属性+方法详解
- ES6 类(Class)基本用法和静态属性方法详解
- ES6 javascript中class静态方法、属性与实例属性用法示例
- es6新特性之 class 基本用法解析
- js中Map之基本用法分开单独写方法,当做类库使用
- jquery中 toggleClass方法的一种用法
- UITextView的基本用法和代理方法
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- Objective-C之foundation框架中的NSString的使用方法、指针基本用法、读取本地和远程文件
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- 面向对象第九课,面向对象三大基本特征,继承,Object用法,toString和equals方法
- jQuery中addClass()方法用法实例
- jQuery中removeClass()方法用法实例
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- jQuery中toggleClass()方法用法实例