js声明对象的方法
中国网络营销推广专家
js声明对象有三种方法:
1.function play(){}
var p=new play();
//添加属性
p.width=300;
p.height=200;
p.num=6;
p.autotime=30;
//添加方法
p.autoplay=function(){
alert('play.......');
}
p.test=function(){
alert('noplay.......');
}
alert(p.width);
p.autoplay();
2.function play(){
var p=new Object();
//添加属性
p.width=300;
p.height=200;
p.num=6;
p.autotime=30;
//添加方法
p.autoplay=function(){
alert('play.......');
alert(p.num);同理于 alert(this.num);//只有静态里面没有this
}
p.test=function(){
alert('noplay.......');
}
return p;
}
var p=play();
alert(p.width);
p.autoplay();
3.function play(width,height,num,autotime){
//添加属性
this.width=width;
this.height=height;
this.num=num;
this.autotime=autotime;
//添加方法
this.autoplay=function(){
alert('play.......');
alert(p.num);同理于 alert(this.num);//只有静态里面没有this
}
this.test=function(){
alert('noplay.......');
}
}
var p=new play();
alert(p.width);
p.autoplay();
这里有一个需要我说明的就是Object这是一个系统自定义的空对象,方便我们的操作。这上面的三种方法,我顺手的是第二种和第三种,我推荐大家使用的也是这两种,希望我们共同来进步,共同来学习