zoukankan      html  css  js  c++  java
  • js reuse code function

    //Classic 1
    function Parent(name) {
    this.name = name || 'Adam';
    }

    Parent.prototype.say = function () {
    return this.name;
    };

    function Child(name) {
    }

    function inherit(C, P) {
    C.prototype = new P();
    C.prototype.constructor = C;
    }

    inherit(Child, Parent);
    var kid = new Child();
    kid.name = 'Rita';
    kid.say();
    //One drawback of this pattern is that you inherit both own properties added to this and
    //
    prototype properties. Most of the time you don’t want the own properties, because
    //
    they are likely to be specific to one instance and not reusable.

    //Classic 2 borrowed constructor pattern
    function Article() {
    this.tags = ['js', 'css'];
    }
    Article.prototype.GetTag = function () {
    return this.tags[0];
    };
    var article = new Article();

    function BlogPost() {
    }
    BlogPost.prototype = article;
    var blog = new BlogPost();

    function StaticPage() {
    Article.call(this);
    }
    var page = new StaticPage();

    alert(article.hasOwnProperty('tags')); // true
    alert(blog.hasOwnProperty('tags')); // false
    alert(page.hasOwnProperty('tags')); //true
    blog.GetTag();
    page.GetTag(); //Error
    /*
    This way you can only inherit properties added to this inside the parent constructor.
    You don’t inherit members that were added to the prototype.
    Using the borrowed constructor pattern, the children objects get copies of the inherited
    members, unlike the classical #1 pattern where they only get references.
    */

    /* 3 Multiple Inheritance by Borrowing Constructors*/
    function Cat() {
    this.legs = 4;
    this.say = function () {
    return "meaowww";
    }
    }

    function Bird() {
    this.wings = 2;
    this.fly = true;
    }

    function CatWings() {
    Cat.apply(this);
    Bird.apply(this);
    }

    var jane = new CatWings();
    console.dir(jane);
    /*The drawback of this pattern is obviously that nothing from the prototype gets inherited
    and, as mentioned before, the prototype is the place to add reusable methods and
    properties, which will not be re-created for every instance.
    A benefit is that you get true copies of the parent’s own members, and there’s no risk
    that a child can accidentally overwrite a parent’s property.
    */

    /*4 Rent and Set Prototype*/
    function Parent(name) {
    this.name = name || 'Adam';
    }
    Parent.prototype.say = function () {
    return this.name;
    };
    // child constructor
    function Child(name) {
    Parent.apply(this, arguments);
    }
    Child.prototype = new Parent();
    var kid = new Child("Patrick");
    kid.name; // "Patrick"
    kid.say(); // "Patrick"
    delete kid.name;
    kid.say(); // "Adam
    /*
    A drawback is that the parent constructor is called twice, so it could be inefficient. At
    the end, the own properties (such as name in our case) get inherited twice.
    */

    /*5 Share the prototype*/
    function inherit(C, P) {
    C.prototype = P.prototype;
    }
    /*that is a drawback because if one child or grandchild
    somewhere down the inheritance chain modifies the prototype, it affects all parents
    and grandparents.
    */

    /*6 A Temporary Constructor*/
    function inherit(C, P) {
    var F = function () { };
    F.prototype = P.prototype;
    C.prototype = new F();
    C.uber = P.prototype;
    C.prototype.constructor = C;
    }
  • 相关阅读:
    [IOI1994][USACO1.5]数字三角形 Number Triangles
    和为给定数
    小凯的疑惑
    棋盘
    【2020NOI.AC省选模拟#2】C. 送分题
    【NOI OL #2】涂色游戏
    【NOI OL #3】小结
    【NOI OL #1】最小环
    【NOI OL #1】冒泡排序
    【NOI OL #1】序列
  • 原文地址:https://www.cnblogs.com/RitaRichard/p/2291350.html
Copyright © 2011-2022 走看看