javascript继承

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript继承脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

javascript是一门比较难精通的语言,原因是其他语言提供机制,javascript提供方法。
在我看来,语言如java,python比较好学的语言有一个特点:关键字多。java提供extends,implements,以及特定的包管理机制。python特殊的文本结构,大量API等。这些语言可以使初学者快速上手,因为大多数需求的满足都是靠机制。语言提供的关键字,库管理,API。一句话就是大家写的代码都一样,思路一样,代码就一样(或差别不大,因为设计模式的存在,有结构差异,但底层结构也相同)。
另一种语言提供作用域,内存布局,语言本身的默认设置等。这种语言如c,c++,javascript,函数式语言。本身是带有一种编译器或解释器的感觉。就是用户身在其中,需要知道一些编译器开发人员才需要的知识。就以javascript来说吧,要想实现继承,你就得了解解释器在读取一个对象的属性的过程中是怎么寻找的(原型链)。c语言:宏,你得了解预处理器;全局变量,连接器;C++:struct和class的区别,vptr,多继承等。
说这么多,只是想为自己花了年学javascript的痛苦吐槽一下,并没有比较语言的意思,莫喷!java那种思路一下,代码差别不大的优势在企业级开发中才能体现,so i love java!绝没有瞧不起的意思。

继承是什么

以java为例

class Parent{

String name; //PRivate field
Parent(String name){
    this.name = name;
}

}

class Child{

int id;  //private field
Child(String name,int id){
    suPEr(name);
    this.id = id;
}

}

继承的重点:

1.Child实例化之前,Parent必须实例化
2.Child对象可以使用Parent对象的方法和属性
3.Child可以添加自己的方法,如果方法名相同,则覆盖Parent的方法
4.Child可以添加自己的静态方法,可以继承Parent的父类方法

简单继承

这也是网上比较多的继承方法(类式继承):

function Parent () {
    this.ParentValue = true;  //private field
}

Parent.staticP = 1;

Parent.prototype.getValue = function () {
    return this.ParentValue;
}

function Child () {
    this.ChildValue = true;  //private field
}

Child.prototype = new Parent();

Child.prototype.getValue = function () {
    return this.ChildValue;
}

Child.staticC = 1;

这种方式不能满足上面的4个条件:
1.满足,但是不是完全相同。Parent在Child定义时就已经实例化了,Parent实例化太超前了,导致所有Child对象同享一个Parent实例,如果一个Child对象改变了Parentvalue,所有Child对象都发生改变。
2.满足
3.满足
4.不满足
所以说基本满足2.5条吧

满足条件的继承

function Parent () {
    this.ParentValue = true;  //private field
}

Parent.staticP = 1;

Parent.prototype.getValue = function () {
    return this.ParentValue;
}

function Child (p,c) {
    Parent.call(this,p);
    this.ChildValue = c;  //private field
}

Child.staticC = 1;
inherITPrototype(Parent,Child);


Child.prototype.getValue = function () {
    return this.ChildValue;
}


function inheritPrototype (Parent,Child) {
    merge(Parent,Child); //将Parent上的属性放置到Parent上
    VAR p = inheritObject(Parent.prototype);
    p.constructor = Child;
    return p;
}

function inheritObject (o) {
    function F(){}
    F.prototype = o;
    return new F();
}

//将Parent上的属性放置到Parent上
function merge (dist,source) {
    for(attr in source){
        if(source.hasOwnProperty(attr)){
            if(!dist.hasOwnProperty(attr)){
                dist[attr] = source[attr];
            }
        }
    }
}

更好的写法

我参考Backbone的实现
Backbone依赖Underscore

function Parent () {
    this.ParentValue = true;  //private field
}
Parent.staticP = 1;


function Child (p,c) {
    Parent.call(this,p);
    this.ChildValue = c;  //private field
}


function extend(Parent,Child,protoProps,staticProps){
    _.extend(Child, Parent, staticProps);

    Child.prototype = _.create(Parent.prototype, protoProps);
    Child.prototype.constructor = Child;

    return Child;
}

extend(Parent,Child,{
    getValue: function() {
        return this.ParentValue;
    }
}, {    
    staticC: 1
});
是不是更方便呢!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript继承全部内容,希望文章能够帮你解决javascript继承所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。