1

私は、オブジェクトのすべてのインスタンス化に渡されるconfigオブジェクトに影響を与えるために、1つの場所で変更しようとしています。オブジェクトがvar myFoo = new Crayons().foo({color: "red"});コンストラクタに渡されるオブジェクトのデフォルトプロパティを設定するにはどうすればよいですか?

と私のプロジェクトに初期化されて、私は誰かがに合格しない場合ように、デフォルトを{color: "blue"}したいのですが

function Crayons(){ 
    return { 
    foo: ThirdPartyFoo 
    } 
} 

次のようにオブジェクトは、グローバルに利用できるようになりますカラー、ブルーが設定されています。

私は

function Crayons(){ 
    var fooWithDefaults = function(){ 
    this = new ThirdPartyFoo(arguments); //this is invalid 
    this.color = "blue"; //and this would overwrite color if it was set 
    } 

    return { 
    foo: fooWithDefaults 
    } 
} 

をやってみました。しかし、私は基本的にthis = new 3rdPartyFooを言うjavascriptのコンストラクタを作成する方法がわからないようnewキーワードは、私を投げています。

私には何が欠けていますか?

+3

'this.color =所与|| "青" ' – Li357

+0

@AndrewL。それは色のために働くが、私はまだオブジェクトを構築する方法を知らない。 – adamdport

+0

まず、 'Crayons'は真のコンストラクタとしてではなく、代わりに工場として実装されています。したがって、 'new'演算子で呼び出す必要はありません。第二に、 'クレヨン'は 'foo'メソッドを持っていません。呼び出された場合、 'ThirdPartyFoo'を参照するプロパティ' foo'を持つオブジェクトを返します。おそらくコンストラクタである可能性があります。あなたの例を少なくとも有効なコードであるものに修正するか、エラーを投げずに実行できるようにしてください( 'var myFoo = new Crayons.foo({color:" red "});') –

答えて

2

あなたはコンストラクタを飾ることができ、次のいずれか

function Crayons(){ 
    function fooWithDefaults() { 
    3rdPartyFoo.apply(this, arguments); // this is what you're looking for 
    if (!this.color) // or whatever to detect "not set" 
     this.color = "blue"; 
    } 
    fooWithDefaults.prototype = 3rdPartyFoo.prototype; // to make `new` work 

    return { 
    foo: fooWithDefaults 
    } 
} 

それともあなたはそれのインスタンスを返すファクトリます

ここ
function Crayons(){ 
    function fooWithDefaults(arg) { 
    var that = new 3rdPartyFoo(arg); // you should know how many arguments it takes 
    if (!that.color) // or whatever to detect "not set" 
     that.color = "blue"; 
    return that; 
    } 

    return { 
    foo: fooWithDefaults 
    } 
} 

var myFoo = Crayons.foo({color: "red"});

を呼び出すときにも newをドロップすることができます

作成後にインスタンスを変更する代わりに、渡されたオプションを装飾することもできます、一般に、より良い解決策である。

function Crayons(){ 
    function fooWithDefaults(arg) { 
    if (!arg.color) // or whatever to detect "not set" 
     arg.color = "blue"; 
    return new 3rdPartyFoo(arg); 
    } 

    return { 
    foo: fooWithDefaults 
    } 
} 
+0

私はリファクタリングを避けようとしているので、 '' new''を削除したくありません。あなたの最初の解決策は私が探していたものです。ありがとう! – adamdport

+0

私は 'fooWithDefaults'の呼び出しで' new'を落とすことができます。あなたはそれを行う必要はありません。私は第3の解決策を最もクリーンなものと考えています。 – Bergi

0

function ThirdPartyCrayon(config) {   // constructor :: possible original Crayon implementation 
 

 
    Object.assign(this, config); 
 
    this.type = "thirdpartycrayon"; 
 
} 
 

 

 
function createCrayonSetting(defaultConfig) { // factory (creates a closure) 
 

 
    function CrayonWithDefaults() {   // constructor :: customized Crayon wrapper 
 

 
     Object.assign(this, defaultConfig); 
 
     ThirdPartyCrayon.apply(this, arguments); 
 
    } 
 

 
    return { 
 
     Crayon: CrayonWithDefaults 
 
    } 
 
} 
 

 

 
var 
 
    setting = createCrayonSetting({color: "blue", strokeWidth: "thin"}), 
 

 
    crayon1 = new setting.Crayon({color: "red", strokeWidth: "bold"}), 
 
    crayon2 = new setting.Crayon({color: "green"}); 
 
    crayon3 = new setting.Crayon(); 
 

 

 
console.log("crayon1 : ", crayon1); 
 
console.log("crayon2 : ", crayon2); 
 
console.log("crayon3 : ", crayon3);

関連する問題