javascript高级属性:私有 和 继承

1。私有实现:
      实现原则:
                ·私有属性可以在构造函数中使用var关键字定义
                ·私有属性只能由特权函数(privileged function)公用访问。特权函数:使用this定义的函数
    示例:
            
function  Vehicle() {
    
var wheelCount = 4;      //私有属性
    
    
this.getWheelCount = function(){      //
特权函数
        return wheelCount;
    }
  
    
    
this.setWheelCount = function(count){
        wheelCount 
= count;
    }
 
}

var  vehicle  =   new  Vehicle();
vehicle.setWheelCount(
5 );
var  wheel  =  vehicle.getWheelCount();
alert(wheel);
2。继承实现
       通过脚本实现。 如
    
function  Vehicle() {
    
var wheelCount = 4;
    
    
this.getWheelCount = function(){
        
return wheelCount;
    }
  
    
    
this.setWheelCount = function(count){
        wheelCount 
= count;
    }
 
}

/* 实现继承  */
function  createInheritance(parent,child) {
    
var property;
    
for(property in parent){
        
if(!child[property])
            child[property] 
= parent[property];
    }

}


function  Child()
}


var  child  =   new  Child();
createInheritance(
new  Vehicle(),child);
alert(child);

3。参考文献:
        《 Ajax.基础教程》第5章
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值