javascript中继承的6种方法及比较

上一篇 / 下一篇  2013-12-11 10:24:54 / 个人分类:javascript

//第一种:原型链继承,其本质是重写了SubPage的原型
//缺点:1 和原型创建对象一样,共享对象类型值(数组)时不是预期
       2 无法传递参数
function Page() {
    this.name = "Super";
}
Page.prototype.getSuperValue = function() {
    return this.name;
}
function SubPage() {
    this.subname = "Sub";
}
//继承:子类型的原型指向超类型
SubPage.prototype = new Page();
//新定义方法或重写超类型的方法时,不可用字面量创建,因为这会重写原型,就切断了和Page原型的联系
SubPage.prototype.getSubValue = function() {
    return this.subname;
}
var instance = new SubPage();
console.log(instance.getSuperValue()); //Super
console.log(instance.getSubValue());  //Sub

//第二种:借用构造函数


TAG:

 

评分:0

我来说两句

Open Toolbar