在JavaScript中,void,eval和Function构造函数之间有什么区别?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在JavaScript中,void,eval和Function构造函数之间有什么区别?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1782字,纯文字阅读大概需要3分钟。
内容图文
![在JavaScript中,void,eval和Function构造函数之间有什么区别?](/upload/InfoBanner/zyjiaocheng/792/ec3f0fc74bf94c698b8b0d066db51f7e.jpg)
void(document.body.innerText += 'hi')
eval(document.body.innerText +='\nbye')
Function(document.body.innerText += '\n!!!')
void(Function(function foo(){document.body.innerText += '\n>hi2'; return true}).toString())();
eval(Function(function foo(){document.body.innerText += '\nbye2'; return true}).toString())();
Function(Function(function foo(){document.body.innerText += '\n!!!2'; return true}).toString())();
在这些不同的语句中执行代码的处理模型是什么?
void(alert('hi'))
undefined
eval(alert('hi'))
undefined
Function(alert('hi'))
function anonymous() {
undefined
}
eval(Function(function foo(){return true}).toString())();
TypeError: undefined is not a function
void(Function(function foo(){return true}).toString())();
TypeError: string is not a function
Function(Function(function foo(){return true}).toString())();
undefined
解决方法:
在this article中,解释了eval和Function构造函数:
(…) Global, built-in
eval
function evaluates code in the scope of a
caller.The code executed from within function created by
Function
constructor
doesn’t really execute in global scope. However, it doesn’t execute in
local scope either, which is what probably leads to confusion.
Function
constructor creates a function whose scope chain consists of
nothing but a global scope (preceded with function’s own Activation
Object, of course). Any code contained in a function created via
Function
constructor evaluates in a scope of that function, not in a
global scope. However, it’s almost as if code executes globally, since
global object is the very next object in the scope chain.
根据this page,void只返回undefined:
In many languages,
void
is a type that has no values. In JavaScript,
void
is an operator that takes an operand and returnsundefined
. This
is not useful, and it is very confusing. Avoidvoid
.
内容总结
以上是互联网集市为您收集整理的在JavaScript中,void,eval和Function构造函数之间有什么区别?全部内容,希望文章能够帮你解决在JavaScript中,void,eval和Function构造函数之间有什么区别?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。