携手走天涯 发表于 2013-1-29 08:29:14

浅谈 javascript 对象的继承方法

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;
}
页: [1]
查看完整版本: 浅谈 javascript 对象的继承方法