zoukankan      html  css  js  c++  java
  • js继承圣杯模型

    js继承模型圣杯模式

    继承模式三段杯,共享原型顶头追。

    中间夹杂隔离敦,防止原型变量混。

    底层原型接上它,在吧构造超类加。

    坐落函数立执行,隔离私有闭包勤。

    继承模式三段杯,共享原型顶头追。

    圣杯模式总体来说分三段

    1,被继承对象

    2,中间层对象

    3,实际继承对象

    中间夹杂隔离敦,防止原型变量混。

    中间层的对象是用来防止底层修改对象属性的时候污染了被继承对象中的属性

    底层原型接上它,在吧构造超类加。

    底层在继承中间层的原型的时候需要把 它的构造函数 以及实际继承的原型存储在中间层中

    <script>

        function inherit(Traget, Origin) {

            //创建中间层构造函数

            function Middle() {

            }

            //让中间层的原型指向 被继承对象的原型

            Middle.prototype = Origin.prototype;

            //让继承对象的原型指向 中间层对象

            Traget.prototype = new Middle();

            //将继承对象的构造器 以及它实际继承的超类存储在中间层

            Traget.prototype.constructor = Traget;

            Traget.prototype.uber = Origin;

        }

        function Father() {

        }

        Father.prototype.name = "father";

        Father.prototype.age = "elder";

        function Son() {

        }

        inherit(Son, Father);

        var son = new Son();

        console.log(son.name + " " + son.age); //father elder

    </script>

    坐落函数立执行,隔离私有闭包勤。

    还有一种是 雅虎提供的 YUI 模式 利用立即执行函数 将中间层私有化

     <script>

        //圣杯模式 YUI

        var cup = (function() {

            //将中间层对象私有化 即和内层函数形成闭包

            function Midle() {};

            function inherit(Target, Origin) {

                Midle.prototype = Origin.prototype;

                Target.prototype = new Midle();

                Target.prototype.constructor = Target;

                Target.prototype.uber = Origin;

            }

            return inherit;

        }());

        function Father() {

        }

        Father.prototype.name = "YUI inherit";

        Father.prototype.age = "yong";

        function GrandSon() {

        }

        cup(GrandSon, Father);

        var grand = new GrandSon();

        console.log(grand.name + " " + grand.age); //YUI inherit yong

    </script>

  • 相关阅读:
    java并发编程(1)并发程序的取消于关闭
    Matlab插值函数
    log4j的配置
    spring-mvc注解(mvc:annotation-driver,JSON,配置详解)
    matlab画图函数plot()/set/legend
    matlab 曲线拟合
    Linux安装库文件(环境变量和makefile)
    css生成彩色阴影
    JSON.stringify()还可以这么用
    ES6中新增的数组知识记录
  • 原文地址:https://www.cnblogs.com/weixin2623670713/p/13587064.html
Copyright © 2011-2022 走看看