Convert Javascript Object (incl. functions) to String

后端 未结 10 1031
你的背包
你的背包 2020-12-29 21:35

Hey, Im trying to convert specific javascript objects to a String. So far I\'m working with json2.js. As soon as my Object contain functions, those functions are stripped. I

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-29 22:10

    The short answer is that you cannot convert arbitrary JavaScript functions to strings. Period.

    Some runtimes are kind enough to give you the string serialization of functions you defined but this is not required by the ECMAScript language specification. The "toString()" example you mentioned is a good example of why it cannot be done - that code is built in to the interpreter and in fact may not be implemented in JavaScript (but instead the language in which the runtime is implemented)! There are many other functions that may have the same constraints (e.g. constructors, built-ins, etc).

提交回复
热议问题