为什么你不能串函数表达式? [英] Why can't you stringify a function expression?

查看:102
本文介绍了为什么你不能串函数表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  console.log(JSON.stringify(function(){console.log ( 'foobar的');})); 


解决方案

JSON不能串联函数,它处理它们就像 undefined null 值。您可以在 EcmaScript 5.1§15.12.3,另请参阅MDN 描述>。



然而,你可以通过将函数表达式转换为字符串来将函数表达式串联起来,尝试

  console.log(+ function(){console.log('foobar');})


Why doesn't this produce anything?

console.log(JSON.stringify(function(){console.log('foobar');}));

解决方案

JSON can't stringify functions at all, it handles them just like undefined or null values. You can check the exact algorithm at EcmaScript 5.1 §15.12.3, see also the description at MDN.

However you of course can stringify function expression by casting them to a string, try

console.log("" + function(){console.log('foobar');})

这篇关于为什么你不能串函数表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆