定义
类是面向对象编程中的一个概念,它包含一些属性和方法。
ES5中使用function关键字,通过构造函数的方式定义类
function Person(name , age) {this.name = name;this.age = age;}Person.prototype.showName = function() {return 'Hello, ' + this.name;}
通过new关键字实例化类,获得一个对象p1
var p1 = new Person('Ken', 19);pi.showName(); //Hello, Ken
ES6中提供了Class关键字来定义一个类,改写上述例子
class Person {construct(name , age) {this.name = name;this.age = age;}showName() {return 'Hello, ' + this.name;}}
