自定义对象:标准写法和白话写法
标准写法:new 实例化
var stu=new Object();
stu.name="毛豆";
stu.sex="男";
stu.age=18;
stu.job="tea";
stu.eat=function (){
//this 指代当前的对象
return this.name+"啥都吃!";
}
//取值
console.log(stu.name);
console.log(stu.eat());
//通过键直接取值
console.log(stu["name"]);
白话写法
//抽象代码 封装对象
var students={
name:"毛豆",
sex:"男",
eat:function (){
return this.name+"啥都吃!";
}
};
students['sleep']=function (){
return this.name+"每天都睡!";
}
console.log(students.name);
console.log(students['sex']);
console.log(students);
console.log(students.sleep());