开发者

How can I set multiple object properties simultaneously with CoffeeScript?

      dbLocation[latitude] = data[1]
      dbLocation[longitude] = data[2]
      dbLocation[locationText] = locationTex开发者_如何学Ct

That's my CoffeeScript, any way to optimize it so it's more condensed?


You can write

obj = {
  latitude: data[1]
  longitude: data[2]
  locationText
}

and then merge that new object in to dbLocation by writing

dbLocation[key] = val for key, val of obj

or using a function like jQuery or Underscore's extend.


Here's a one-liner, but it's not really much more readable:

[dbLocation.latitude, dbLocation.longitude, dbLocation.locationText] = [data[1], data[2], locationText]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜