javascript高级属性:私有 和 继承

    技术2022-05-11  59

    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章

    最新回复(0)