2016-08-16 7 views
0

私は、ビュー(HTMLマークアップ)とユーティリティ(JavaScript - ビヘイビアー)アーキテクチャーと、ES6クラスを使用してビューとユーティリティを構成するためのアトミッククラスを作成しています。単一のビュークラスに複数のユーティリティクラスを作成/混合する必要があります。ES 6クラス - ミックスイン

どのようにしてES6クラスAPIを使用して、クラスを別の/メインクラスに混在させることができますか。私はObject.assignを見てきましたが、それはオブジェクトのためであり、クラスレベルではありません。

答えて

2

JavaScriptクラスと今後もうまくいけば のみを拡張することができますが、 を互いに組み合せることはできません。まったくの場合は、おそらく軽量の特性 は1日以内に指定してください。

アーキテクチャアプローチはJavaScript固有のものです。それ ここ数年でかなり頻繁に言及されています... esdiscuss.org:»about lightweight traits«、 github.com/WebReflection:»features :: with«、 webreflection.blogspot.com:»A future friendly, backward compatible, class utility«、 reddit.com/r/javascript:»Functional Mixins in ECMAScript 2015« 、 raganwald.com:»Functional Mixins in ECMAScript 2015« ... とおそらくアンガス・クロールのFlight Mixinsと比較しています。

ミックスイン/形質が近づいベースピュア機能... This is not an essay about 'Traits in Javascript'The many »Talents« of JavaScript ... OPはのようなもの ない限りを求める持っているものに近い来るのかは...

// proposed trait syntax ...  // ... desugared e.g. to ... 

trait Enumerable_first_last {  // var Enumerable_first_last = (function() { 
    // trait body.     // // mixin module. 
            // 
    const        // var 
    FIRST = function() {   //  first = function() { // shared code. 
     return this[0];    //  return this[0]; 
    },        //  }, 
    LAST = function() {   //  last = function() { 
     return this[this.length - 1]; //  return this[this.length - 1]; 
    }        //  } 
    ;         // ; 
            // 
    applicator() {     // return function Enumerable_first_last() { 
    // applicator body.    //  // mixin body. 
            // 
    this.first = FIRST;    //  this.first = first; // referencing ... 
    this.last = LAST;    //  this.last = last; // ... shared code. 
    }         // }; 
            // 
}         // }()); 

...

// proposed trait syntax ...  // ... desugared e.g. to ... 

trait Enumerable_item {    // var Enumerable_item = (function() { 
            // 
    const        // var 
    ITEM = function (idx) {   //  item = function (idx) { 
     return this[     //  return this[ 
     Math.floor(    //   Math.floor(
      parseFloat(idx, 10)  //   parseFloat(idx, 10) 
     )       //   ) 
     ];       //  ]; 
    }        //  } 
    ;         // ; 
            // 
    applicator() {     // return function Enumerable_item() { 
            // 
    this.item = ITEM;    //  this.item = item; 
    }         // }; 
            // 
}         // }()); 

...

// proposed trait syntax ...  // ... desugared e.g. to ... 

trait Enumerable_first_last_item { // var Enumerable_first_last_item = (function() { 
            // 
    use Enumerable_first_last;  // return function Enumerable_first_last_item() { 
    use Enumerable_item;    // 
/*         //  Enumerable_first_last.call(this); 
    applicator() {     //  Enumerable_item.call(this); 
    // can be omitted if empty.  // }; 
    }*/        // 
}         // }()); 
         // ... desugared e.g. to ... 
             // 
class Queue {       // var Queue = (function() { 
             // 
//use Allocable;      // return function Queue() { 
    use Observable;      //  var list = []; 
             // 
    constructor() {     //  this.enqueue = function (type) { 
    const list = [];     // 
             //  list.push(type); 
    this.enqueue = function (type) { //  return type; 
             //  }; 
     list.push(type);    //  this.dequeue = function() { 
     return type;     // 
    };        //  return list.shift(); 
    this.dequeue = function() {  //  }; 
             // 
     return list.shift();   // //Allocable.call(this, ...); 
    };        //  Observable.call(this); 
    }         // }; 
             // 
}          // }()); 
             // 
var q = new Queue;     // var q = new Queue; 
             // 
q.enqueue(9);       // q.enqueue(9); 
q.enqueue(8);       // q.enqueue(8); 
q.enqueue(7);       // q.enqueue(7); 
             // 
console.log(q.dequeue());    // console.log(q.dequeue()); 
console.log(q.dequeue());    // console.log(q.dequeue()); 
console.log(q.dequeue());    // console.log(q.dequeue()); 
             // 
console.log(q);      // console.log(q); 
console.log(Object.keys(q));   // console.log(Object.keys(q)); 

は... ... ECMAScriptの土地に出荷されました。

+0

const Foo = base => class extends base { myFn() { } }; const Bar = base => class extends base { myFn() { super.myFn(); } }; class Baz extends mixinClasses(Foo, Bar) { myFn() { super.myFn(); } } 
1/2 - 上記スケッチ形質構文を証明するために、私はLIBを提供し、例示的にどのARA(1)COM、あまりにもJavaScriptのQsの関連する4他の形質のコードをリファクタリングましたJSのミックスイン](http://stackoverflow.com/questions/41999608/compostions-and-mixins-in-js/43141778#43141778)、(2)[バベルで蒸解されたES6クラスのミックスイン](http: //stackoverflow.com/questions/30732241/mixins-for-es6-classes-transpiled-with-babel/43129978#43129978)、... –

+0

2/2 - (3)[レガシーmixinベースのクラス階層のリファクタリング](http://stackoverflow.com/questions/43027388/refactoring-legacy-mixin-based-class-hierarchies/43059101#43059101) [クラスを使った多重継承](http://stackoverflow.com/questions/41918874/multiple-inheritance-using-classes/43748183#43748183) –

2

私は少し馴染みのあるmixin by Sebastian Markbageを作成するために、ES2015クラス(これは必ずしもそうであるとは限りません)で本当にいいパターンがあります。

ユーティリティ関数mixinClassesはあなたの新しいクラスにクラスファクトリ(クラスを返す別名ファクトリ関数)で混合するために使用することができます。

次のように2つの工場の機能を持つ例えば、使用することができ
function mixinClasses(...mixins) { 
    // TODO: Add all possible method names that might call super() 
    // to the base class so that they don't throw. 
    return mixins.reduce((base, mixin) => { 
     return mixin(base); 
    }, class {}); 
} 

FooBar

関連する問題