1

What is the syntax needed to include a function within a json attribute object that is used to populate a data attribute? If I quote the function, its typeof === 'string', but I want typeof to yield 'function'. Here are two examples:

<input type="text" data-element='{"setter": setData(id, key, value),"another": "value"}'>

The above example won't run - javascript cannot parse it. The below example yields typeof 'string'

<input type="text" data-element='{"setter": "setData(id, key, value)", "another": "value"}'
4

1 回答 1

1

You can't do this; JSON is a lightweight data-interchange format and only supports the following:

  • Number
  • String
  • Boolean
  • Array
  • Object (a collection of key:value pairs, comma separated and enclosed in curly braces)
  • null

http://www.json.org

http://en.wikipedia.org/wiki/JSON

于 2013-10-07T23:34:05.047 回答