js中的對(duì)象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>js中的對(duì)象</title>
<script>
//定義一個(gè)學(xué)生的對(duì)象
var student={
//屬性
name:"張三",
age:18,
//方法
eat:function(){
return "這是一個(gè)吃飯的方法";
},
sleep:function(){
return "這是一個(gè)睡覺的方法";
}
}
//對(duì)象的屬性和方法的調(diào)用
document.write(student.name);
document.write(student["age"]);
document.write(student.eat());
document.write(student.sleep());
document.write("----------------------------------------<br>");
var student={
"name":"張三",
"age":18,
"eat":function(){
return "這是一個(gè)吃飯的方法";
},
"sleep":function(){
return "這是一個(gè)睡覺的方法";
}
}
//對(duì)象的屬性和方法的調(diào)用
document.write(student.name);
document.write(student["age"]);
document.write(student.eat());
document.write(student.sleep());
document.write("----------------------------------------<br>");
</script>
</head>
<body>
</body>
</html>
js對(duì)象中的屬性和方法的調(diào)用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js對(duì)象中的屬性和方法的調(diào)用</title>
<script>
//定義一個(gè)圖書對(duì)象
var books={
name:"JAVA",
published:2018,
author:{
firstName:"張三",
secondName:"李四",
thirdName:"王五"
},
display:function(){
return "我是圖書展示功能";
}
}
//第一種調(diào)用方式
document.write(books.name);
document.write(books.author.firstName);
//第二種調(diào)用方式
document.write(books["published"]);
document.write(books["author"]["secondName"]);
//第三種,混合調(diào)用
document.write(books["author"].thirdName);
//調(diào)用方法
document.write(books.display());
</script>
</head>
<body>
</body>
</html>
js對(duì)象中屬性與方法的修改
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js對(duì)象中屬性與方法的修改</title>
<script>
var person={
name:"張三",
addr:"北京",
say:function(){
return "說話的是:" + this.name;
}
}
document.write(person.say());
//修改對(duì)象屬性
person.name="李四";
document.write(person.say());
//添加對(duì)象屬性
person.age=18;
document.write(person.age);
//刪除對(duì)象屬性
delete person.addr;
document.write(person.addr);
//添加對(duì)象方法
person.newFun = function(){
alert("這是個(gè)新方法");
}
person.newFun();
//對(duì)方法重寫
person.newFun=function(){
alert("我是新方法渣慕,我被重寫了");
}
person.newFun();
</script>
</head>
<body>
</body>
</html>
使用object關(guān)鍵字構(gòu)造對(duì)象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>使用object關(guān)鍵字構(gòu)造對(duì)象</title>
<script>
//使用object關(guān)鍵字定義對(duì)象
var carBen = new Object();
//添加屬性
carBen.name="奔馳";
carBen.color="黑色";
carBen.displacement=2.0;
//第一種添加方法
carBen.run = runMethod;
function runMethod(){
document.write("最高時(shí)速250km");
}
//第二種方式添加方法
carBen.run2 = function(){
document.write("最高時(shí)速350km");
}
//調(diào)用對(duì)象的屬性和方法
carBen.run();
carBen.run2();
document.write("這是一輛"+carBen.name+"训裆,顏色是:"+carBen.color+",排量是"+carBen.displacement)
</script>
</head>
<body>
</body>
</html>
使用構(gòu)造器函數(shù)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>構(gòu)造器函數(shù)</title>
<script>
//使用function關(guān)鍵字創(chuàng)建一個(gè)對(duì)象類型(構(gòu)造函數(shù))
function Car(Name,Color,Displacement){
this.name=Name;
this.color = Color;
this.displacement=Displacement;
this.run = carRun;//給對(duì)象添加方法
this.run2 = function(){document.write("時(shí)速1000km");}
}
//定義一個(gè)方法
function carRun(){
document.write("時(shí)速600km");
}
//構(gòu)造新對(duì)象
var carTe=new Car("特斯拉","紅色",0.5);
document.write("這是一輛"+carTe.name+"豺谈,顏色是:"+carTe.color+",排量是"+carTe.displacement);
carTe.run();
carTe.run2();
</script>
</head>
<body>
</body>
</html>