1.创建对象的三种方式
1.1字面量方式
let json = {
name: '张三',
age: 12,
sex: '男',
eat: function () {
console.log(this);
console.log('吃的方法');
}
}
json.eat();
let func = json.eat;
func();
1.2调用系统构造函数
let obj = new Object;
obj.name = '张三';
obj.age = 16;
obj.sex = '男';
obj.eat = function () {
console.log('吃的方法');
};
console.log(obj);
1.3自定义构造函数
function Person(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
this.play = function () {
console.log("天天打游戏");
};
}
let p = new Person("雏田", 18, "女");
console.log(p);
class People {
constructor(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
play = () => {
console.log(this);
}
}
console.log(new People("雏田", 18, "女"));