温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

javascript的对象继承有几种方法?

发布时间:2020-05-23 09:55:54 来源:亿速云 阅读:266 作者:Leah 栏目:web开发

javascript的对象继承有几种方法?相信很多人对javascript的对象继承方法的了解处于一知半解状态,小编给大家总结了以下内容。如下资料是关于javascript的对象继承方法的内容。

1原型链继承
所有的javascript 都会继承一个prototype原型对象  继承原型对象上的属性和方法。
例如:
date  对象  从Date.prototype 原型对象上继承属性和方法
array 对象  从Array.prototype  原型对象上继承属性和方法
要继承  必须有父类  ----子类继承父类  继承父类的属性和方法

特点:实例可以继承自身实例的属性   父类里面的构造函数属性  父类的原型属性
缺点:不能继承子类的原型属性和方法
太单一   只能单继承  不能进行多继承
继承之后原型对象上的属性全部是共享的
子类不能直接向父类传递参数

function Person() {
        this.name;
        this.sex;
        this.sleep = function () {
            return "睡觉";
        };
        this.eat = function () {
            return "吃饭"
        }
    }
        //给父类添加原型属性和方法
    Person.prototype.job = function () {
        return "工作";
    };
    Person.prototype.color = "黄";
        function Child() {
        this.age = 20;
    }
        Child.prototype = new Person();  //核心   让子类的原型 等于 父类的实例对象
         var child = new Child();
         console.log(Child.prototype);//继承之后指向Person  对象
          console.log(child instanceof Child);  ///true
    console.log(child instanceof Person);  //true

2构造函数
直接使用call  apply  继承
构造继承直接在子类的内部去写
优点:  可以实现多继承;可以向父类传递参数
缺点:  子类的实例是本身 不是父类;构造继承只能call  apply  父类对象的构造属性和方法  不能复制原型属性和方法

 //父类
    function Animail(s, a) {
        this.sex = s;
        this.age = a;
        this.sleep = function () {
            return "睡觉";
        }
    }
    //Animal  添加原型属性
    Animail.prototype.color = "花色";
    //动物类别类
    function Type(t) {
        this.type = t;
    }
    //子类
    function Cat(n, s, a, t) {
        this.name = n;
        this.eat = function () {
            return "吃东西"
        }
        Animail.call(this, s, a);
        Type.apply(this, [t]);
    }

    //实例化子类对象
    var cat = new Cat("小猫", "公", 2, "猫科");  //类对象在实例化的时候会直接执行自身的构造函数
    console.log(cat);
        /检测构造继承里面的类别问题
    console.log(cat instanceof Cat);//true
    console.log(cat instanceof Animail);//false
    console.log(cat instanceof Type);//false

3实例继承
原理是在子类里面直接构造父类的实例
优点:可以传递给父类参数    不限制调用的方式
缺点:不能多继承;不能拿到子类的构造属性和方法
实例继承 子类的实例不是本身而是父类

 //父类
    function Person(n, s) {
        this.name = n;
        this.sex = s;
        this.sleep = function () {
            console.log(this.name + "睡觉");
        }
    }
    //子类
    function Child(n, s) {
        var per = new Person(n, s);
        return per;
    }
    //实例化子类对象
    var child = new Child("张三", "女");
         console.log(child instanceof Child);//false
    console.log(child instanceof Person);//true

4拷贝继承
原理是 将父类里面的属性方法拷贝给子类
子类的实例是本身  不是父类
子类向父类传递参数
可以支持多继承

function Animal(n) {
        this.name = n;
        this.sleep = function () {
            return this.name + "睡觉"
        }
    }
    function Cat(n, a) {
        this.age = a;
        //拷贝
        var animal = new Animal(n);
        for (var p in animal) {
            Cat.prototype[p] = animal[p];
        }
        //继续new 别的对象  进行拷贝
    }
    /* Cat.prototype.name="";
     Cat.prototype.sleep=function (){

     };*/
    //实例化子类
    var cat = new Cat("小花", 3);
    console.log(cat);
         console.log(cat instanceof Cat);//true
    console.log(cat instanceof Animal);//false

5组合继承
构造继承+原型链继承
子类的实例即是本身也是父类
没有原型对象属性的共享
实现多继承
调用了两次父类的构造函数

    function Person(n) {
        this.name = n;
        this.sleep = function () {
            return this.name + "睡觉";
        }
    }
    Person.prototype = {
        job: function () {
            return this.name + "job";
        }
    }

    function Child(n, a, s) {
        this.age = a;
        this.sex = s;
        //构造继承
        Person.call(this, n);
    }
    //原型链继承
    Child.prototype = new Person();
    var child = new Child("张三", 18, "男");
        console.log(child);
        console.log(child instanceof Child); //true
    console.log(child instanceof Person);//true

6寄生组合继承
是处理组合继承的缺点  避免两次调用父类的构造函数
原理是把父类的原型给予一个空对象的原型
子类对象的实例即是本身也是父类

function Person(n) {
        this.name = n;
        this.sleep = function () {
            return this.name + "睡觉";
        }
    }
    console.log(Person.prototype);

    function Child(n, a) {
        this.age = a;
        this.eat = function () {
            return this.name + "吃饭"
        }
        Person.call(this, n);
    }
    //寄生
    (function () {
        var fn = function () {
        };
        //将父类的原型对象给予空对象
        fn.prototype = Person.prototype;
        Child.prototype = new fn();
    })();

    var child = new Child("李四", 20);
    console.log(child);
        console.log(child instanceof Child); //true
    console.log(child instanceof Person); //true

关于javascript的对象继承的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果喜欢这篇文章,不如把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI