How to use hyphen in object property name?

后端 未结 2 1420
深忆病人
深忆病人 2021-01-18 01:56

$(\'.class\').data({data-toggle: \"whatever-value\", data-target: \"#id\"});

I keep getting syntax errors for the -

I originally t

2条回答
  •  自闭症患者
    2021-01-18 02:27

    Just to add to what's alredy been pointed out, in Javascript, objects have properties defined by arbitrary strings.

    obj.prop is really just sugar for obj["prop"], with the latter being more expressive because all characters are allowed in the latter. Likewise, {prop: "val"} is really sugar for {"prop": "val"}. This is just one of many cases where you'll have to resort to the desugared syntax.

提交回复
热议问题