js继承

//js继承
function classA(msg)
{
    this.age = 23;
    this.name = 'luowen';
    if(typeof classA._initialed == 'undefined')
        classA.prototype.sayHi = function(){alert('hello');}
    classA.initialed = true;

}

//原型冒充方法
function classB(msg,age)
{
    this.newMethod = classA;
    this.newMethod(msg);
    delete this.newMethod;
    //overwrite age property
    this.age = age;
}

//call方法实现
function classB(msg,age)
{
    classA.call(this,msg);
    this.age = age;
}

//apply方法实现
function classB(msg,age)
{
    classA.apply(this,arguments);
    this.age = age;
}

//原型链继承,确保没有参数
function classB(){
}
classB.prototype = new classA();
var oClassB = new classB();
console.log(oClassB.sayHi());

  

posted @ 2014-04-26 12:14  arvim  阅读(151)  评论(0编辑  收藏  举报