Why can't you stringify a function expression?

后端 未结 5 1439
醉话见心
醉话见心 2020-12-30 01:06

Why doesn\'t this produce anything?

console.log(JSON.stringify(function(){console.log(\'foobar\');}));
5条回答
  •  时光说笑
    2020-12-30 02:02

    JSON has no means to represent a function. It is a data format designed for simplicity and compatibility across languages (and a function is the last thing that will be cross-language compatible).

    From the docs for JSON.stringify:

    If undefined, a function, or an XML value is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).

提交回复
热议问题