zoukankan      html  css  js  c++  java
  • inheritprototype原型继承封装及综合继承最简实例

    1、inheritprototype.js

    ;(function(){
        var s = {
            inheritObject:function(o){//对象继承封装
                var F = function(){};
                F.prototype = o;
                return new F();
            },
            inheritPrototype:function(subclass,supperclass){//原型继承封装
                var obj = this.inheritObject(supperclass.prototype);
                obj.constructor = subclass;
                subclass.prototype = obj;
            }
        };
        window.$ = window.s = s;//起别名并把闭包内的命名空间对象s暴露出去
    })(window);

    2、inheritprototype.html

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/inheritprototype.js"></script>
    </head>
    <body>

    </body>
    <script type="text/javascript">
    ;(function(){
        function Shape(){//超类
            this.name = 'Lucy';
            this.type = '超体者'
        }
        Shape.prototype = {
            init:function(){
                var name = this.getname();
                var type = this.gettype();
                console.log(name);
                console.log(type);
            },
            getname:function(){
                return this.name;
            },
            gettype:function(){
               return this.type;
            }
        }
        function Triangle(){//子类
            Shape.apply(this);//this继承构造体
            this.sex = '女';
        }
        $.inheritPrototype(Triangle,Shape);//这里一定要注意,是先继承再拓展自己的原型方法,否则报错
        Triangle.prototype.getsex = function(){
           console.log(this.sex);
        }
        var o = new Triangle();
        o.init();//继承父元素的init()并执行
        o.getsex();//执行Triangle构造函数的方法
    })();
    </script>
    </html>

  • 相关阅读:
    AcWing 1027. 方格取数 dp
    AcWing 1014. 登山 dp
    acwing 482. 合唱队形 dp
    LeetCode 1463. 摘樱桃II dp
    LeetCode 100. 相同的树 树的遍历
    LeetCode 336. 回文对 哈希
    LeetCode 815. 公交路线 最短路 哈希
    算法问题实战策略 DARPA大挑战 二分
    算法问题实战策略 LUNCHBOX 贪心
    AcWing 1100. 抓住那头牛 BFS
  • 原文地址:https://www.cnblogs.com/koleyang/p/5501636.html
Copyright © 2011-2022 走看看