最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using function parameter value as dictionary key - Stack Overflow

programmeradmin0浏览0评论

Attempting to build a dictionary using key that es via function parameter.

var progres_mark = function(progress_state) {
  var now = Date();
  console.log({ progress_state : now })
}

progres_mark("encode")

Expected

{ 'encode': 'Sun Oct 19 2014 18:22:33 GMT+0300 (IDT)' }

Actual

{ progress_state: 'Sun Oct 19 2014 18:22:33 GMT+0300 (IDT)' }

What’s going on?

Attempting to build a dictionary using key that es via function parameter.

var progres_mark = function(progress_state) {
  var now = Date();
  console.log({ progress_state : now })
}

progres_mark("encode")

Expected

{ 'encode': 'Sun Oct 19 2014 18:22:33 GMT+0300 (IDT)' }

Actual

{ progress_state: 'Sun Oct 19 2014 18:22:33 GMT+0300 (IDT)' }

What’s going on?

Share Improve this question asked Oct 19, 2014 at 15:38 Maxim VekslerMaxim Veksler 30.2k42 gold badges134 silver badges153 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Because the piler only expects an identifier or a string and therefore will not evaluate to the variable's value. But you can use bracket notation to achieve what you want.

var progres_mark = function(progress_state) {
  var now = Date();
  var obj = {}; obj[progress_state] = now;
  console.log(obj)
}
发布评论

评论列表(0)

  1. 暂无评论