日期:2014-05-16 浏览次数:20504 次
function testCls(a){
this.a=a;
}
testCls.prototype.alertA=function(){
alert(this.a);
}
var test=new testCls(5);
test.alertA(); //弹出5
alert(test.a); //同样弹出5
var testCls=function(arg1){
var a=arg1;
var b=10;
var alertB=function(){
alert(b);
}
this.c=5;
this.setA=function(v){
a=v;
};
this.alertA=function(){
alert(a);
};
this.alertB=function(){
alertB();
}
}
var test=new testCls(4);
test.setA(6);
test.alertA();
test.alertB();
var testCls=function(arg1){
//对象内部公有变量,不允许外部访问
var a=arg1;
var b=10;
var alertB=function(){
alert(b);
}
//外部可访问区域
return {
c:5,
setA:function(v){
a=v;
},
alertA:function(){
alert(a);
},
alertB:function(){
alertB();
}
}
};
var test=new testCls(4);
test.setA(6);
test.alertA();
test.alertB();