1. 对象字面量
let obj = {
name: 'siri',
age: 12
}
2. new Object();
let obj = new Object();
obj.name = 'siri'
obj.age = 12
3. 构造函数
function Star (name, age) {
this.name = name;
this.age = age;
this.sing = function () {
console.log("我会唱歌");
}
}
let siri = new Star("siri", 12);
4. es6 中的 class
class Star {
constructor (name, age) {
this.name = name;
this.age = age;
}
sing () {
console.log("我会唱歌");
}
}
let siri = new Star("siri", 12);