js实例对象是类的真实对象
举例:
ES5:
// ES5:生成实例对象的传统方法是通过构造函数
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
}
Person.prototype.sayName = function () {
alert(this.name);
};
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27, "Doctor");
ES6:
// ES6可通过class关键字定义类
class Person {
constructor(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
}
sayName() {
alert(this.name);
}
}
// ES6使用类时也是直接对类使用new命令
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27, "Doctor");
结论:person1和person2都是“Person类”的实例对象(实例都是对象)