您的位置:首页 > Web前端 > JavaScript

js继承

2014-04-26 12:14 309 查看
//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());


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: