JS继承

作者: 饥人谷_米弥轮 | 来源:发表于2017-11-05 02:51 被阅读15次

1.基本原型链继承

<script type="text/javascript">
    //animal 父类 超类
    var Animal = function (name) {
        this.name = name;
        this.sayhello = function () {
            alert("HI,我是" + this.name + ",你愿意和我做朋友吗?");
        };
    };
    Animal.prototype.shout = function () {
        alert(this.name + ",正在叫!");
    };
    Animal.prototype.game = function () {
        alert(this.name + ",正在玩耍!");
    };

    var Dog = function (name) {
        // this.name = name;    //重写父类属性

        // IE不支持Dog.prototype.__proto__ 这种继承方式,会报错

        Dog.prototype.name = name;  //继承父类属性
        this.shout = function () //重写父类的函数
        {
            alert(this.name + ",正在汪汪叫!");
        }
        console.log(this);
    };
    //原型继承
    Dog.prototype = new Animal();

    var dog = new Dog("小黑");
    dog.sayhello();
    dog.shout();
    dog.game();
</script>

2.封装成方法的原型链继承

<script type="text/javascript">
    // 原型继承可以在Function这个对象中添加一个方法,可以方法会继承到所有的类或者函数当中,当需要的时候直接调用
    Function.prototype.extends = function (superclass) {
        this.prototype = new superclass();
    };

    //animal 父类 超类
    var Animal = function (name) {
        this.name = name;
        this.sayhello = function () {
            alert("HI,我是" + this.name + ",你愿意和我做朋友吗?");
        };
    };
    Animal.prototype.shout = function () {
        alert(this.name + ",正在叫!");
    };
    Animal.prototype.game = function () {
        alert(this.name + ",正在玩耍!");
    };

    var Dog = function (name) {
        this.name = name;
        this.shout = function () //重写父类的函数
        {
            alert(this.name + ",正在汪汪叫,叫的很开心!");
        };
    };
    Dog.extends(Animal);    //调用在开头Function对象中添加的方法,实现继承


    var Husky = function (name, color, sex) {
        this.name = name;
        this.color = color;
        this.sex = sex;
        this.sayhello = function () {
            alert("Hello,我是一条" + this.sex + "的狗,有一个非常好听的名字,叫:“" + this.name + "”,你愿意和我做朋友吗?");
        };
        this.showcolor = function () {
            alert(this.color);
        };
        /*this.shout = function()//重写父类的函数
        {
            alert(this.name + ",哼哼叫!");
        };*/
    };
    Husky.extends(Dog);

    var xh = new Husky("小哈", "黑白", "雄性");
    xh.sayhello();
    xh.shout();
    xh.game();
    xh.showcolor();
</script>

3.call\apply继承

<script type="text/javascript">
    var Animal = function (name) {
        this.name = name;
        this.sayhello = function () {
            alert("Hello!我是" + this.name + ",你愿意和我做朋友吗?");
        };
        this.shout = function () {
            alert(this.name + ",正在叫!");
        };
        this.game = function () {
            alert(this.name + ",正在玩耍!");
        };
        console.log(this);
    };

    /*Animal.prototype.shout = function()
    {
        alert(this.name + ",正在叫!");
    };
    Animal.prototype.game = function()
    {
        alert(this.name + ",正在玩耍!");
    };*/  //无法继承通过原型链新增的方法

    var Dog = function (name, color) {
        //Animal.apply(this, [name]);

        Animal.call(this, name);
        this.color = color;
        this.sayhello = function () //同样可以覆盖父类(超类)中的函数(方法)
        {
            alert("Hello!我是" + this.name + ",你看看我的" + this.color + "色好看吗??");
        };
        this.showcolor = function () //同样可以自己发展出父类中所没有的属性或函数
        {
            alert("我是一条" + this.color + "色的小狗狗~~");
        };
    };

    //Dog.prototype = new Animal();

    var xh = new Dog("小黑", "黑白");
    xh.sayhello();
    xh.showcolor();
    xh.shout();
    xh.game();

    /*
    call和apply只作用于方法(函数),这种方式继承相当于浅继承,只会继承函数当前的方法(函数),而通过prototype原型链添加的方法(函数)无法继承,也无法继承属性;call和apply函数只是实现了方法的替换,而没有对对象的属性和函数进行复制操作。
    
    原型继承,则可以继承所有的属性和函数。
    */
</script>

相关文章

  • Js的继承

    js的继承 @(js)[继承, js, 前端] 组合继承是原性链继承和构造函数继承的合体,它汲取了二者各自的有点,...

  • JS继承

    JS中的继承 许多OO语言都支持两种继承方式:接口继承和实现继承; 因为JS中没有类和接口的概念 , 所以JS不支...

  • #js继承

    js继承的概念 js里常用的如下两种继承方式: 原型链继承(对象间的继承)类式继承(构造函数间的继承) 类式继承是...

  • js继承遇到的小问题

    这两天在看js继承方面,它不像OC那种传统的类继承。js继承方式还是挺多的。比如:原型继承、原型冒充、复制继承 原...

  • JS中继承的实现

    JS中继承的实现 #prototype (js原型(prototype)实现继承) 全局的Function对象没有...

  • js继承

    js继承js 继承-简书 原型链实现集继承 上面的代码实现原型链继承最重要的son.prototype=new f...

  • JavaScript 10

    js继承的概念 1.通过原型链方式实现继承(对象间的继承) 2.类式继承(构造函数间的继承) 由于js不像Java...

  • JS继承的实现的几种方式

    前言 JS作为面向对象的弱类型语言,继承也是非常强大的特性之一,那么如何在JS实现继承呢? JS继承的实现方式 既...

  • 2019-03-25 继承

    js中通过原型来实现继承 组合继承:原型继承+借用构造函数继承

  • 继承方式(6种)1.7

    JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一。那么如何在JS中实现继承呢?让我们拭目以待。 JS继承...

网友评论

    本文标题:JS继承

    本文链接:https://www.haomeiwen.com/subject/xnifmxtx.html