随着2015年6月ECMAScript 6的发布,引入了Javascript类语法.
这个语法:
class Polygon {
constructor(width, height) {
this.width = width;
this.height = height;
}
}
Run Code Online (Sandbox Code Playgroud)
基本相同:
function Polygon(width, height) {
this.width = width;
this.height = height;
}
Run Code Online (Sandbox Code Playgroud)
那么使用类而不是传统函数有什么好处呢?在什么条件下我应该使用类而不是函数?