Pau*_*tte 5 javascript eval void ecmascript-5 function-constructor
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())();Run Code Online (Sandbox Code Playgroud)
在这些不同的语句中执行代码的处理模型是什么?
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
Run Code Online (Sandbox Code Playgroud)
在这篇文章eval中,和Function构造函数进行了解释:
\n\n\n(\xe2\x80\xa6) 全局内置
\n\neval函数计算调用者范围内的代码\n。从构造函数创建的函数内执行的代码
\nFunction\n并不真正在全局范围内执行。但是,它也不会在本地作用域中执行,这可能会导致混乱。Function构造函数创建一个函数,其作用域链仅由全局作用域组成(前面是函数 xe2 \x80\x99 拥有自己的 Activation\n 对象,当然)。Function通过构造函数创建的函数中包含的任何代码都 在该函数的作用域中计算,而不是在全局作用域中计算。然而,它几乎就像代码在全局执行一样,因为全局对象是作用域链中的下一个对象。
根据此页面,void仅返回undefined:
\n\n在许多语言中,
\nvoid是一种没有值的类型。在 JavaScript 中,\nvoid是一个运算符,它接受一个操作数并返回undefined。这\n没有用,而且很混乱。避免void。
| 归档时间: |
|
| 查看次数: |
1509 次 |
| 最近记录: |