JSのTOOLSを作成JavaScript動的オブジェクトクラス
角度1.5.xの& lodash 4.xの
QUESTION私はオブジェクトを作成するオブジェクトファクトリパターンを作成しようとしてい
/クラス 。以下の私の例では、コンテンツタイプのオブジェクトモデルを作成しています。私のスキルの限界を考えると、私は以下の記事の例をすべてのコンテンツタイプに対して複製しています。独自のプロトタイプを持つ動的オブジェクトクラスを作成する方法はJavaScriptにありますか?
現在のアプローチ
// constructor injection
_Constructor.$inject = [];
function _Constructor() {
function __constructor(data, keys) {
_.assign(this, _.pick(data, keys));
}
return __constructor;
}
// one of many content type models
Article.$inject = ['__constructor'];
function Article(__constructor) {
function Article(data) {
var fillables = Object.freeze(['id','title','subtitle','body','body']);
__constructor.call(this, data, fillables);
}
Article.prototype = Object.create(__constructor.prototype);
Article.prototype.constructor = Article;
return Article;
}
IDEALアウトカム
私はユニークなコンテンツタイプのオブジェクトモデルを作成することを可能にするコンテンツタイプのモデル工場のパターンを作成します。擬似例:
var documentFillables = Object.freeze(['id', 'name', 'body']),
documentData = {id:1,name:'My Document', body: 'coolbody stuff'},
Document = new ModelFactory('Document', documentData, documentFillables),
articleFillables = Object.freeze(['id', 'title', 'subtitle']),
articleData = {id:1,title:'My Article', subtitle: 'My Subtitle'},
Article = new ModelFactory('Article', articleData, articleFillables);
私はマージの周りプレイしたノート、拡張し、クローンと私はコードの冗長性のかなりの量および/または汎用オブジェクトすなわちで終わるオブジェクトを複製して拡張することができますががModelFactory()
多くを持っていますが、 no Article()
。