- 爱易网页
 
                        - 
                            JavaSript
 
                        - 浅谈 javascript 对象的沿袭方法 
 
                         
                    
                    
                    日期:2014-05-16  浏览次数:20431 次 
                    
                        
                         浅谈 javascript 对象的继承方法
    [size=large]1.对象冒充
function Father(name) {
    this.name = name;
    this.sayName = function() {
         alert(this.name);
    }
}
function Child(name,age) {
    this.method=Father;
    this.method(name);
    delete this.method;
    this.age = age;
    this.sayAge = function() {
        alert(this.age);
    }
}
2.用call方法实现
function Father(name) {
    this.name = name;
    this.getName =function () {
        return this.name;
    }
}
function Child(name,password) {
     Father.call(this,name);
     this.password = password;
     this.getPassword=function() {
         return this.password;
    }
}
3.使用原型链方式
function Father() {
}
Father.prototype.name = "zhangsan";
Father.prototype.getName=function() {
      return this.name;
}
function Child() {
}
Child.prototype = new Father();
Child.prototype.password = "134";
Child.prototype.getPassword=function() {
    return this.password;
}[/size]
                        
                     
                    
                    
                        免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。