html
<html>
<p id="colors"></p>
</html>
1JavaScript
<javascripy>
var colo={
blue:"蓝色",
green:"绿色", //对象属性
black:function(){ //对象方法
return "黑色"
}
}
document.getElementById("colors").innerHTML=colo.blue//访问对象属性
//访问对象方法 document.getElementById("colors").innerHTML=colo.black()
</JavaScript>
2JavaScript
<javascripy>
var colo=new Object();
colo.blue="蓝色";
colo.green="绿色";
colo.black=function(){
return "黑色"
}
document.getElementById("colors").innerHTML=colo.blue//访问对象属性
//访问对象方法 document.getElementById("colors").innerHTML=colo.black()
</JavaScript>
3JavaScript
<javascripy>
function yanse(){
var colo=new Object();
colo.blue="蓝色";
colo.green="绿色";
colo.black=function(){
return "黑色"
}
return colo//返回colo给yanse()函数,所以var yanses=yanse()即是var yanses=colo
}
var yanses=yanse()
document.getElementById("colors").innerHTML=yanses.blue//访问对象属性
//访问对象方法 document.getElementById("colors").innerHTML=yanses.black()
</JavaScript>
4JavaScript
<javascripy>
function yanse(){
this.blue="蓝色";
this.green="绿色";
this.black=function(){
return "黑色"
}
}
var yanses=new yanse()
document.getElementById("colors").innerHTML=yanses.blue//访问对象属性
//访问对象方法 document.getElementById("colors").innerHTML=yanses.black()
</JavaScript>