1.字面量方式
var person = {
name: '张三',
age: '18',
sex: 'man',
play: function () { //方法
console.log('打乒乓');
}
}
2、new Object()创建对象
//另一种方法 中括号
person['height'] = '170'
person[height] = '170'
var person = new Object();
person.age = '18'
person.name = '张三'
person.sex = 'man'
person.play = function () {
console.log('打乒乓');
}
3、工厂模式创建对象
function person(name, age, sex) {
var person = new Object();
person.age = age
person.name = name
person.sex = sex
person.play = function () {
console.log('打乒乓');
}
return person
}
var person1 = person('张三',18,'man')
var person2 = person('小丽',18,'girl')
4、构造函数(最常用)
function Person(age, name, sex) {
this.name = name
this.age = age
this.sex = sex
this.play = function () {
console.log('打乒乓');
}
}
var person1 = new Person(18, '张三', 'man');
var person2 = new Person(19, '小丽', 'girl');