Giter Site home page Giter Site logo

Comments (7)

pumpkin123 avatar pumpkin123 commented on May 16, 2024 12

node 跑了�一下,是a is not defined
不太明白为啥

from all-of-frontend.

huangpingcode avatar huangpingcode commented on May 16, 2024

报错Uncaught ReferenceError: a is not defined

因为如果你间接的使用 eval(),比如通过一个引用来调用它,而不是直接的调用 eval。 从 ECMAScript 5 起,它工作在全局作用域下,而不是局部作用域中,MDN

from all-of-frontend.

chaifuling avatar chaifuling commented on May 16, 2024

报错Uncaught ReferenceError: a is not defined
image
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.

turkyden avatar turkyden commented on May 16, 2024

Marked

from all-of-frontend.

Chrisyjs avatar Chrisyjs commented on May 16, 2024

from all-of-frontend.

dorothyLee56 avatar dorothyLee56 commented on May 16, 2024

会报错:Uncaught ReferenceError: a is not defined
通过引用调用eval,执行上下文是全局作用域。

from all-of-frontend.

SunshineZJJ avatar SunshineZJJ commented on May 16, 2024

报错:a is not undefined,
因为自执行匿名函数作用域是window,而函数test里面声明的a,作用域是函数test内部,作用域不同所以报错

from all-of-frontend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.