JS继承的六种方法代码实现

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JS继承的六种方法</title>
    <script>

        /* ------------------原型链继承------------------ */
        // 父类构造函数
        function SuperType() {
            this.property = true;
        }

        // 父类原型方法
        SuperType.prototype.getSuperValue = function () {
            return this.property;
        }

        //子类构造函数
        function SubType() {
            this.subproperty = false;
        }

        // 子类原型对象指向父类实例实现继承
        SubType.prototype = new SuperType();

        SubType.prototype.getSubValue = function () {
            return this.subproperty;
        }

        var sub = new SubType();

        console.log(sub.getSuperValue());
        /* ------------------原型链继承------------------ */

        /* ------------------借用构造函数继承------------------ */
        function SuperType() {
            this.colors = ["red", "blue", "green"];
        }

        function SubType() {
            SuperType.call(this);
        }

        var sub1 = new SubType();
        sub1.colors.push("black");
        console.log(sub1.colors); // ["red", "blue", "green", "black"]
        var sub2 = new SubType();
        console.log(sub2.colors); // ["red", "blue", "green"]


        // 例2
        function Person(name, age, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;

        }

        Person.prototype.sayName = function () {
            alert(this.name);
        }// 相对子类实例不可见,不可用

        function Student(name, age, sex, weight) {
            Person.call(this, name, age, sex);

            this.weight = weight;
        }

        var s1 = new Student("梁真真", 23, "女", "47kg");

        console.log(s1);

        /* ------------------借用构造函数继承------------------ */

        /* ------------------组合继承------------------ */

        function Person(name, age, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.friends = ["王华", "李明"];
        }

        Person.prototype.sayName = function () {
            alert(this.name);
        }

        function Student(name, age, sex, height) {
            Person.call(this, name, age, sex);
            this.height = height;
        }

        Student.prototype = new Person();
        Student.prototype.sayHeight = function () {
            alert(this.height);
        }
        var s1 = new Student("王钱青", 24, "男", "186cm");

        console.log(s1);


        /* ------------------组合继承------------------ */

        /* ------------------原型式继承------------------ */

        var person = {
            name: "梁真真",
            friends: ["wqq", "wnf", "wqq"]
        };
        var p1 = Object.create(person, {
            name: {
                value: "汪曾祺"
            }
        });

        console.log(p1);

        /* ------------------原型式继承------------------ */

        /* ------------------原型式继承------------------ */

        function object(o) {
            function F() {
            }

            F.prototype = o;
            return new F();
        }

        function createAnother(ori) {
            var clone = object(ori);

            clone.sayHi = function () {
                alert("Hi");
            };

            return clone;
        }

        var person = {
            name: "wqq",
            friends: ["lzz", "zjs", "wzq"]
        };
        var anotherPerson = createAnother(person);
        console.log(anotherPerson);

        /* ------------------原型式继承------------------ */


        /* ------------------寄生组合式继承------------------ */

        function object(o) {
            function F() {
            }

            F.prototype = o;
            return new F();
        }

        function inheritPrototype(superType, subType) {
            var prototype = object(superType.prototype);
            prototype.constructor = subType;
            subType.prototype = prototype;
        }

        function SuperType(name) {
            this.name = name;
            this.colors = ["red", "blue", "green"];
        }

        SuperType.prototype.sayName = function () {
            alert(this.name);
        };

        function SubType(name, age) {
            SuperType.call(this, name);
            this.age = age;
        }

        inheritPrototype(SuperType, SubType);   // 这一句,替代了组合继承中的SubType.prototype = new SuperType()

        SubType.prototype.sayAge = function () {
            alert(this.age);
        };

        var sub1 = new SubType("wq", 24);
        console.log(sub1);


        /* ------------------寄生组合式继承------------------ */

    </script>
</head>
<body>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值