Code Monkey home page Code Monkey logo

Comments (6)

deqwin avatar deqwin commented on June 3, 2024 1

(function(){
console.log(a);
var a = 1;
})()

这里应该是打印出undefined,不是1

from canjs.

jrainlau avatar jrainlau commented on June 3, 2024

注意这一句scope = scope.parentScope。由于Scope是一个实例对象,所以当执行到这一句的时候,this所指向的scope其实已经是parentScope

from canjs.

deqwin avatar deqwin commented on June 3, 2024

我试了一下好像不是耶?

var x = 1;
if (true) {
var y = 2;
if (true) {
var z = 3;
}
}
console.log(x+y+z);

就用这个例子测试。y=2成功赋值到父作用域是es5.js line 22

nodeIterator.scope.parentScope.declare(name, value, kind)

这行的作用,不是issue中的递归实现的,所以z=3并不能成功赋值到最外层的作用域上。

from canjs.

deqwin avatar deqwin commented on June 3, 2024

另外还有个疑问:BlockStatement中是不是在声明提前的过程中,不小心把变量赋值也提前了?

from canjs.

jrainlau avatar jrainlau commented on June 3, 2024

我试了一下好像不是耶?

var x = 1;
if (true) {
var y = 2;
if (true) {
var z = 3;
}
}
console.log(x+y+z);

就用这个例子测试。y=2成功赋值到父作用域是es5.js line 22

nodeIterator.scope.parentScope.declare(name, value, kind)

这行的作用,不是issue中的递归实现的,所以z=3并不能成功赋值到最外层的作用域上。

已经在最新的commit里面修复了。确实是varDeclaration函数的问题。

至于你的第二个问题没有很懂,有具体代码体现吗?

from canjs.

JonathanLee-LX avatar JonathanLee-LX commented on June 3, 2024

(function(){
console.log(a);
var a = 1;
})()

这里应该是打印出undefined,不是1

var a = 1这行代码对应的AST

{
    "type": "VariableDeclaration",
    "start": 3,
    "end": 12,
    "declarations": [ {
      "type": "VariableDeclarator",
      "start": 7,
      "end": 12,
      "id": {
        "type": "Identifier",
        "start": 7,
        "end": 8,
        "name": "a"
      },
      "init": {
        "type": "Literal",
        "start": 11,
        "end": 12,
        "value": 1,
        "raw": "1"
      }
    } ],
    "kind": "var"
  }

所以在声明的时候会直接求值,并且赋值
const value = declaration.init ? nodeIterator.traverse(declaration.init) : undefined
如果要正确的实现js的声明提前特性,可能需要将字符串形式的代码进行一次预编译,将var a = 1这样的语句编译成

var a = undefined
a = 1

from canjs.

Related Issues (11)

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.