自定义对象:
?1:使用Object()构造函数
<script>
var student=new Object();
student.name="张三";
student.age=18;
student.showInfo=function(){//匿名函数
return "姓名:"+student.name+",年龄:"+student.age;
}
alert(student.showInfo());
</script>
<script>
var student=new Object();
student.name="张三";
student.age=18;
student.showInfo=function(name){//匿名函数,传参
return "姓名:"+name+",年龄:"+student.age;
}
alert(student.showInfo("李四"));
</script>
2:对象直接量
<script>
var student={
name:'张三',
age: 18,
showInfo:function(){
return "姓名:"+student.name+",年龄:"+student.age;
}
};
alert(student.showInfo());
</script>
如何调试错误:点击F12
?
?面试题:创建Person对象,并在网页下输出以下信息
?
?
<script>
var person={
name:"周更生",
sex:"男",
age:30,
acdemic:"大专",
money:2000,
salary:500,
money2:2500,
home:"山东省",
showInfo:function(){
return "姓名:"+person.name+"性别:"+person.sex+"年龄:"+person.age+"学历:"+person.xueli+"工资"+person.money;
}
};
alert(person.showInfo());
</script>
?JS内建对象:
?1:String对象
|