对象的理解
用{ }表示,里面以键值对的形式存储元素,一般有变量和方法,可以用点语法来访问内部元素。增删改查代码示例如下:
var teacher = {
name: '张三',
age: 30,
sex: '男',
hoppy: ['篮球', '音乐'],
teach: function (sub) {
var sub = arguments[0] || 'JS';
console.log('I am teaching' + sub);
},
eat: function () {
console.log('I like eat eggs');
}
}
console.log(teacher.teach());
console.log(teacher);
//增
teacher.height = 175;
teacher.drink = function () {
console.log('I like drink cola');
}
//删
delete teacher.age;
delete teacher.eat;
//改
teacher.name = '李四';
teacher.teach = function (){
console.log('I am teaching HTML');
}
//查
console.log(teacher.teach());
console.log(teacher);
console.log(teacher.hoppy);