日期:2014-05-16  浏览次数:20359 次

浅谈 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]