【问题标题】:JavaScript property access: dot notation vs. brackets?JavaScript property access: dot notation vs. brackets?
【发布时间】:2022-12-02 13:13:04
【问题描述】:

Other than the obvious fact that the first form could use a variable and not just a string literal, is there any reason to use one over the other, and if so under which cases?

In code:

// Given:
var foo = {\'bar\': \'baz\'};

// Then
var x = foo[\'bar\'];

// vs. 
var x = foo.bar;

Context: I\'ve written a code generator which produces these expressions and I\'m wondering which is preferable.

标签: javascript syntax object-literal


【解决方案1】:
【解决方案2】:
【解决方案3】:
【解决方案4】:
【解决方案5】:
【解决方案6】:
【解决方案7】:
【解决方案8】:
【解决方案9】:
【解决方案10】:
【解决方案11】:
【解决方案12】:
【解决方案13】:
【解决方案14】:
【解决方案15】:
【解决方案16】:
【解决方案17】:
猜你喜欢
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 2019-11-07
  • 2012-08-10
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多