简介:JavaScript中对象的两种定义方法
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>2016-04-06 JavaScript-对象</title>
<!--CSS-->
<style>td{border:1px solid red;text-align:center;width:50px;}</style>
<script>
//对象:定义方法1
var person={
name:"Tom",
age:"20",
sex:"M"
};
//对象:定义方法2
var person1=new Object();
person1.name="John";
person1.age="22";
person1.sex="W";
</script>
</head>
<body>
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
</tr>
<tr>
<td id="name"></td>
<td id="age"></td>
<td id="sex"></td>
</tr>
<tr>
<td id="td4"></td>
<td id="td5"></td>
<td id="td6"></td>
</tr>
</table>
<script>
//利用for..in..遍历对象属性
for(var x in person){
document.getElementById(x).innerHTML=person[x];
}
document.getElementById("td4").innerHTML=person1.name;
document.getElementById("td5").innerHTML=person1.age;
document.getElementById("td6").innerHTML=person1.sex;
</script>
</body>
</html>