Javascript:「クラス」と継承

Javascriptのクラス自体の概念の欠如にいつも少し腹を立てていましたが、継承をすると、一般的に少し緊張します。 このトピックに関する非常に多くの資料を読み、コメントで与えられたajaxoop.jsライブラリーのわずかに切り捨てられたバージョンを含む、 ここからいくつかの「回避策」を試し後、適切で馴染みのあるものは見つかりませんでした。

ただし、「馴染みのある」継承の実装を必要とするタスクがあり、その追加の快適な副作用は多重継承の可能性でした。

だから、私はこの問題に対する私の意見の解決策でこのようなかなり簡単なものを提案します:
/** *    . *     . * : * function MyClass(arg1, arg2) { * ExtClass.call(this, { * MyParentClass1: [arg1], //   1   * MyParentClass2: [arg2], //   2 * MyParentClass3: [arg1, arg2], //    * ... * }); * * //        * //  * } * *      MyClass      *   MyParentClass1  .. *        ,  MyClass *         *    . *           *    : * this.$super['MyParentClassName'].methodName.call(this, ...args...); *         this.$super */ function ExtClass(supers) { //       //        ( , //   ),      this.$super = this.$super ? this.$super : {}; //        //    this.instanceOf() this.$_parents = this.$_parents ? this.$_parents : []; /** * ,       * @param string className     * @returns boolean TRUE  ,  FALSE */ this.instanceOf = function(className) { return (__inArray(className, this.$_parents) || (this instanceof eval(className))); }; /** *      . *  .     : * this.$super['parenClassName'].method.call(this, ...args...); * @param object that   this  * @param string className   */ function __addSuper(that, className) { var obj = new (eval(className)); that.$super[className] = {}; if (!__inArray(className, that.$_parents)) that.$_parents.push(className); for (i in obj) { if (typeof obj[i] == 'function') { that.$super[className][i] = obj[i]; } } }; /** *          * @param mixed value   * @param array arraySeek   * @returns boolean TRUE  ,  FALSE */ function __inArray(value, arraySeek) { if (arraySeek && arraySeek.length) { for (i in arraySeek) { if (arraySeek[i] == value) return true; } } return false; }; //      for (i in supers) { var className = i; var args = supers[i]; (eval(className)).apply(this, args); __addSuper(this, className); } }; 

たとえば、2つの親クラスを作成します。
 function Parent1() { ExtClass.call(this); //    this.message = function() { return 'Parent1::message'; } } function Parent2() { ExtClass.call(this); //    this.message = function() { return 'Parent2::message'; } } 

そして、それぞれが親から継承する2つのクラスの子クラス:
 function Child1() { ExtClass.call(this, { Parent1: null }); this.message = function() { return 'Child1::message'; } } function Child2() { ExtClass.call(this, { Parent2: null }); this.message = function() { return 'Child2::message'; } } 

さて、子クラスから継承する要約クラス:
 function Child12() { ExtClass.call(this, { Child1: null, Child2: null }); this.message = function() { //         var message = this.$super['Parent1'].message.call(this) + "\n"; message += this.$super['Parent2'].message.call(this) + "\n"; message += this.$super['Child1'].message.call(this) + "\n"; message += this.$super['Child2'].message.call(this) + "\n"; //     ) message += 'Child12::message' return message; } } 

最後のクラスのオブジェクトを作成し、このオブジェクトのmessage()メソッドが返されることを確認します。
 var childTest = new Child12(); alert(childTest.message()); 

結果は次のとおりです。
Parent1::message
Parent2::message
Child1::message
Child2::message
Child12::message

ご覧のとおり、すべての親メソッドは適切に機能します。 このソリューションが多くの人に役立つことを願っています。


Source: https://habr.com/ru/post/J128000/


All Articles