首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

浅谈 javascript 对象的沿袭方法

2013-01-04 
浅谈 javascript 对象的继承方法[sizelarge]1.对象冒充function Father(name) {this.name namethis.sa

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

热点排行