Combine a variable with variable value in Javascript

蹲街弑〆低调 提交于 2019-12-02 18:19:33

问题


I am trying to do something like this in Angular javascript (a simplified code):

var modelName = "date";

if (attrs.hasOwnProperty('today')) {
   scope.modelName = new Date();
}

In the above, I actually want scope.modelName to become scope.date automatically. How can I parse the modelName variable to its value?


回答1:


You can access properties of objects using square brackets.

var modelName = "date";

if (attrs.hasOwnProperty('today')) {
   scope[modelName] = new Date();
}


来源:https://stackoverflow.com/questions/22671770/combine-a-variable-with-variable-value-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!