1.对象可以用来保存多个值。
2.需要多个类似对象时可以使用构造函数:
//构造函数
function Star(uname,sex,age){
this.uname = uname;
this.sex = sex;
this.age = age;
this.sing = function(song){
console.log(song);
}
}
var zxy = new Star('zhangxueyou','male','18');
console.log(zxy.uname);
zxy.sing('wenbie');
- 遍历对象
//遍历对象
var zxy = {
name: 'zhangxueyou',
sex: 'male',
age: 18,
sing: function(song){
console.log(song);
}
}
for(k in zxy){
console.log(k);//k输出变量名
console.log(zxy[k]);//对象[k]输出值
}