<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
/*********************************創建對象***********************************/
//方法一:
//創建學生對象
var student={
studentName:'黃裳',
age:'70',
speak:function(){
return '能說英語。';
}
}
//方法二:
var teacher=new Object();
teacher.teacherName='韓立';
teacher.age='2000';
teacher.speak=function(){
return '能鬥法。';
}
//方法三:
//注意:
//1、推薦構造函數首字母大寫;
//2、this表示當前對象;
//3、用構造函數創建對象要用new。
function CreateTeacher(teacherName,age){
this.teacherName=teacherName,
this.age=age,
this.fun=function(a){
return a;
}
}
var ct1=new CreateTeacher('zhangsan','20');
/*********************************獲取對象屬性***********************************/
//獲取屬性方法一:
console.log(student.age);
console.log(student.speak());
console.log(student.studentName);
//獲取屬性方法二:
console.log(teacher['age']);
console.log(teacher['teacherName']);
console.log(ct1.age);
console.log(ct1.teacherName);
console.log(ct1.fun('能工作'));
/*********************************遍歷對象屬性***********************************/
console.log('遍歷對象屬性');
for(k in student){
//屬性名
console.log(k);
//屬性值
console.log(ct1[k]);
}
</script>
</head>
<body>
</body>
</html>