
本文共 2205 字,大约阅读时间需要 7 分钟。
静态方法和属性
直接上代码吧
//静态成员的例子
varHouse=(function(){
//私有的静态属性
varnumOfHouse=0;
//私有的静态方法
functionisValid(hid){
if(hid!=null&&hid!=undefined&hid!=""){
returntrue;
}
else{
returnfalse;
}
}
//返回构造器
returnfunction(hid,hname,address){
//私有属性
var_hid,_hname,_address;
this.getHid=function(){
return_hid;
}
this.setHid=function(hid){
if(!isValid(hid)){
_hid=hid;
}
}
this.getHname=function(){
return_hname;
}
this.setHname=function(hname){
_hname=hname||"暂无";
}
this.getAddress=function(){
return_address;
}
this.setAddress=function(address){
_address=address||"暂无";
}
numOfHouse++;
if(numOfHouse>2)
thrownewError("House:Only 2 House can be created");
this.setHid(hid);
this.setHname(hname);
this.setAddress(address);
};
})();
//公共的静态方法
House.washWindow=function(){
alert("Window Washed!");
}
//Public ,non-privileged method
House.prototype={
showHouse:function(){
document.getElementById("container").innerHTML+="静态楼盘:"+this.getHname()+"<br />";
}
}
//静态方法调用
functionstaticHouse(){
try{
varstaticHouse1=newHouse('123','HouseName - 第一个','Beijing');
alert(staticHouse1.getHname());
staticHouse1.showHouse();
varstaticHouse2=newHouse('123','HouseName - 第二个','Beijing');
alert(staticHouse2.getHname());
staticHouse2.showHouse();
varstaticHouse3=newHouse('123','HouseName','Beijing');
}
catch(e){
alert(e);
}
}
利用之前学过的作用域以及Closures的方法,我们能够创建既能够被公共访问也能够被私有访问的静态成员。可以说,静态成员的操作都是在类层级上而不是实例层级上。上面例子中最关键的两点:一是构造器放在return中,另外一个是最后跟的一对空的括号,这就使得返回的构造器得到了立即的执行;
常量
常量不过就是不能改变的变量,在JavaScript中,我们可以通过创建私有的变量来模拟常量。
直接上代码吧,这个也比较容易看懂,为类定义了一些常量。
//常量的简单示例
varClass=(function(){
varCLASSNAME="I'm Jack!";
varCREATEDATE="I'm born on Sep 10!";
varHOMETOWN="I'm from 北京";
varconstants={
HEIGHT:100,
WIDTH:400
}
this.getName=function(){
returnCLASSNAME;
}
this.getConstants=function(name){
returnconstants[name];
}
returnfunction(){
this.showName=function(){
alert(getName());
};
this.showConstant=function(name){
alert(getConstants(name));
}
};
})();
functionuseConstant(){
varcl=newClass();
cl.showName();
cl.showConstant("HEIGHT");
}
封装的好处:
封装保证了内部数据的完整性,只允许访问器和存取器来访问数据,这样来保证对数据保存和返回的完整控制。这就减少了我们在其他地方对于数据合法性检查的代码。封装还可以使你的对象尽量的保持独立,这就减少了紧耦合,而这正是面向对象设计的最重要的一条原则。通过封装,你的代码的复用性提高了,而你可以很容易的把他们清理出去。
封装的坏处:
由于内部的方法和变量都是隐藏的,所以对封装过的对象做单元测试变得困难。
发表评论
最新留言
关于作者
