ES5 对象的写法
let x = 10,y = 20;const obj = {x: x,y: y,sum: function () {return this.x + this.y;}
};
ES6 对象的写法
const obj = {x,y,sum() {return this.x + this.y;}
};
class
有点类似 java 的 class
class Person { // 类名大写// 私有属性和方法 写在 construtor 里面constructor(name, age) {this.name = name;this.age = age;this.showName = function () {console.log(this.age);}}// class 内部的原型里面只允许写方法sayName() {console.log('I am ' + this.name);}
}const person = Person('mary', 19)
constructor
里面是对象的私有属性和私有方法 类里面是原型上的方法