Comments (7)
node 跑了�一下,是a is not defined
不太明白为啥
from all-of-frontend.
报错
Uncaught ReferenceError: a is not defined
因为如果你间接的使用 eval(),比如通过一个引用来调用它,而不是直接的调用 eval。 从 ECMAScript 5 起,它工作在全局作用域下,而不是局部作用域中,MDN
from all-of-frontend.
报错Uncaught ReferenceError: a is not defined
mdn 上eval 上说明
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval
也就是说间接调用 eval函数的指向还是在全局声明的作用 不能访问局部变量
function test() {
const a = 1
const b = 2
return function() {
const c = 3
eval('console.log(a,b,c)')
}
}
test()()
所以 eval 不能做间接函数的调用 , 建议直接调用 避免出现错误
from all-of-frontend.
Marked
from all-of-frontend.
- 结果:Uncaught ReferenceError: a is not defined
- 原因:间接使用 eval,例如通过引用来调用,它工作在全局作用域下,所以访问的是全局作用域下的变量( https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval )
from all-of-frontend.
会报错:Uncaught ReferenceError: a is not defined
通过引用调用eval,执行上下文是全局作用域。
from all-of-frontend.
报错:a is not undefined,
因为自执行匿名函数作用域是window,而函数test里面声明的a,作用域是函数test内部,作用域不同所以报错
from all-of-frontend.
Related Issues (20)
- Potential security issue
- Add social banner. HOT 1
- 项目地址挂了
- 前端进阶之道网站怎么在出售中?
- 第十九题:JS 中的数据存放位置 HOT 21
- 第二十题:实现深拷贝函数 HOT 17
- 第二十一题:Vue 有了数据劫持为什么还要 DOM diff? HOT 11
- 第二十二题:0.1 + 0.2 !== 0.3? HOT 20
- 第二十三题:普通函数和箭头函数的区别是什么? HOT 30
- 第二十四题:Babel 会将箭头函数编译成什么? HOT 19
- 第二十五题:三问闭包 HOT 22
- 第二十六题:如何实现 sleep 函数 HOT 25
- 第二十七题:开发中有遇到过比较难定位的问题吗? HOT 12
- 第二十八题:以下代码输出什么?为什么? HOT 17
- 关于为什么出现域名以及为什么域名和ip并用的场景
- 第三十一题:将48位的时间位图格式化成字符串
- 插眼 HOT 1
- 如何自学的图片为空白内容 HOT 2
- 画图工具的使用问题 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from all-of-frontend.