Comments (6)
(function(){
console.log(a);
var a = 1;
})()
这里应该是打印出undefined,不是1
from canjs.
注意这一句scope = scope.parentScope
。由于Scope
是一个实例对象,所以当执行到这一句的时候,this
所指向的scope
其实已经是parentScope
了
from canjs.
我试了一下好像不是耶?
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.
另外还有个疑问:BlockStatement中是不是在声明提前的过程中,不小心把变量赋值也提前了?
from canjs.
我试了一下好像不是耶?
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.
(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
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 canjs.