JavaScript之对象

创建对象的实例需要使用new关键字,就像java一样。
访问属性:对象实例.属性名       或者        对象实例["属性名"]
访问方法:对象实例.方法名


自定义对象:其实就是方法,不过在实例化的时候加上new
function Play(){}
var  play = new Play();

 

第一种创建对象的方法:
function Play(){}
var  play = new Play();
play.width = 10;    //添加属性
play.getWidth=function(){};    //添加方法


第二种创建对象的方式:

function fun(){
var v = new Object()
v.width = 300;
v.getWidth = function(){this.width //访问width属性}
return v;
}

var f = new fun();
alert(f.width)

 

第三种创建对象的方式:

function fun1(){
this.width = 300;
this.getWidth = function(){this.width //访问width属性}
}

var f = new fun1();
alert(f.width)
//遍历属性,及其值
for(var pro in f){
  alert(f[pro])  //值
}

 

posted on 2013-01-28 17:12  lee0oo0  阅读(177)  评论(0)    收藏  举报