对象
1. 对象的创建方法
- 对象字面量/对象直接量: var obj = {}
var mrDeng = {
name : 'MrDeng',
age : '40',
sex : 'male',
wife : 'xiaoWang',
health : 100,
smoke : function(){
console.log('I am smoking!');
this.health --;
},
drink : function(){
console.log('I am dring');
this.health ++;
}
}
- 系统自带的构造函数 : Object()
var obj = new Object();
obj.name = 'yaoyaole';
obj.age = 40;
- 自定义
function Car(color){
this.color = color;
this.name = 'BMW';
this.height = '1400';
this.lang = '4900';
this.weight = 1000;
this.health = 100;
this.run = function(){
this.health --;
}
}
var car1 = new Car();
var car2 = new Car();
car1.name = 'Maserati';
car2.name = 'Merz'
2. 对象的属性
- 查看对象(可在控制台直接查看)
console.log(mrDeng);
- 查看属性(可在控制台直接查看)
console.log(mrDeng.wife);
- 修改/添加属性(控制台可操作,无法保存,和html,css一样)
mrDeng.wife = 'xiaoLiu'
- 删除属性
delete mrDeng.name;
|