SO私はのようになりますJSオブジェクト..動的にJavaScriptでオブジェクトのプロパティを設定する方法
var Monitor=function(parent,params={}){
this.parent=null;
this.canvas=null;
this.width=600;
this.height=400;
this.backColor="#252525";
this.lineColor="#0171a7";
this.lineWidth=4;
this.radius=3;
/* 2017-12-31 **********************************
Innitialize the monitor class
***********************************************/
this.init=function(parent,params){
var that=this;
this.parent=parent;
//Loop through params and set them.
$.each(params,function(i,val){
eval("that."+i+"="+val);
})
return this;
};
this.init(parent,params);
}
を持っていると私は設定したいと...
mc=new Monitor(
$("#monitors"),
{
"width":800;
"height":600,
}
);
それを呼び出しますループ内のプロパティを動的に変更します。
しかし、それを動作させるには、私はeval(Evalは悪い...右か?)を使用する必要があります。 プロパティを動的に設定するより良い方法はありますか?
を経由して渡されたオプションは、車輪の再発明しないで、機能はそのためにあります: '$ .extend(これ、のparams)'や 'Object.assign(これをチェック、params) 'in vanilla JS(ES6) – Thomas
[名前として変数を使用してJavaScriptオブジェクトにプロパティを追加するにはどうすればよいですか?](https://stackoverflow.com/questions/695050/how-do-i-add-a-property-to- a-javascript-object-a-variable-as-the-name) –