Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2015年3月6日 作用域 #42

Open
nunnly opened this issue Mar 6, 2015 · 19 comments
Open

2015年3月6日 作用域 #42

nunnly opened this issue Mar 6, 2015 · 19 comments
Labels

Comments

@nunnly
Copy link
Owner

nunnly commented Mar 6, 2015

function a(x,y){
    y = function(){x=2;};
    return function(){
        var x = 3;
        y();
        console.log(x);
    }.apply(this,arguments);
}
a();
//求最后的输出
//题目来自
//https://gist.github.com/ruanyf/cae49b92b0bd43c4d57d
@nunnly nunnly added the 难度5 label Mar 6, 2015
@XadillaX
Copy link
Collaborator

XadillaX commented Mar 6, 2015

个明显是 3 啊,怎么会是难度 5,有什么解释的,var 都写在上面了还有什么好解释的。

@think2011
Copy link
Collaborator

求解释,求解释!
@XadillaX

@think2011
Copy link
Collaborator

function foo() {
    console.log( a ); // 2
}

function bar() {
    var a = 3;
    foo();
}

var a = 2;

bar(); //2

看了这个就明白了。

@XadillaX
Copy link
Collaborator

XadillaX commented Mar 6, 2015

foo 和 bar 不在一个作用域好么,foo 能找到的 a 是最外层的,又找不到 bar 里面的。@think2011

明明是很简单的问题,为什么一定要搞复杂了 -。 -

@think2011
Copy link
Collaborator

function a(x, y) {
    y = function() {
        x = 2;
    };
    return function() {
        x = 3;
        y();
        console.log(x);
    }.apply(this, arguments);
}
a(); // 2

@XadillaX 因为作为菜鸟容易把事情复杂化..

@VaJoy
Copy link

VaJoy commented Mar 6, 2015

-。- 好简单的都懒得回答了,话说大神@XadillaX 终于舍得回来了

@XadillaX
Copy link
Collaborator

XadillaX commented Mar 6, 2015

@think2011

x 始终都是传进来的——一开始是 undefined,然后 x = 3,然后在 y 里面被等于 2,最后输出就是 2 了。

@XadillaX
Copy link
Collaborator

XadillaX commented Mar 6, 2015

@VaJoy

最近忙成狗了 -。 -

看到前两天传得比较火的阮老师的问题,实际上有点小时候做应用题的混淆法,根本就是那么点东西,加了一堆没用的混淆条件。

@VaJoy
Copy link

VaJoy commented Mar 6, 2015

@XadillaX 辛苦啦,有时间去做大保健,别累坏了 →。→

@think2011
Copy link
Collaborator

@XadillaX 恩。明白了。

  • a() 之前, xy 都初始化成 undefined
  • y() 修改的 x 是上述的 x
  • 最后 return 的 匿名函数,如果不声明 xx 是 上述的 x,如果声明了,则是此作用域下的 x

@XadillaX
Copy link
Collaborator

XadillaX commented Mar 6, 2015

@think2011 就是从本作用域下冒泡往上找,找到的第一个就是了。

@VaJoy
Copy link

VaJoy commented Mar 6, 2015

其实这题稍微改一下可以继续混一点变量提升的概念,23333

function a(x,y){
    y = function(){x=2;};
    return function(){
        y();
        console.log(x);
        var x = 3;     //move it here
    }.apply(this,arguments);
}
a();

@XadillaX
Copy link
Collaborator

XadillaX commented Mar 6, 2015

@VaJoy 这个倒是比之前的有意思些,其实主要是变量声明会被提前。

@XadillaX
Copy link
Collaborator

XadillaX commented Mar 6, 2015

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

var 写前面后面都没关系,只要有就会在一开始被声明。

上面的结果输出的就是 2 1。

@VaJoy
Copy link

VaJoy commented Mar 6, 2015

@XadillaX 但是我修改后的那段代码运行后是undefined...

@XadillaX
Copy link
Collaborator

XadillaX commented Mar 6, 2015

@VaJoy 你那段代码实际上是:

function a(x,y){
    y = function(){x=2;};
    return function(){
        var x;
        y();
        console.log(x);
        x = 3;     //move it here
    }.apply(this,arguments);
}
a();

这一点在我

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

这段代码里面就有体现。

@VaJoy
Copy link

VaJoy commented Mar 6, 2015

@XadillaX 嗯嗯是滴,提前声明了但没提前赋值

@VaJoy
Copy link

VaJoy commented Mar 6, 2015

玩一玩ES6吧,这算不算bug呢?@XadillaX

"use strict";
function a(x,y){
    y = function(){x=2;};
    return function(){
        y();
        console.log(x);
        let x = 3;     //use let
    }.apply(this,arguments);
}
a();

iojs运行后报错说x没定义过不能执行console.log(x)
但注释掉 let x = 3 后就成功打印“2”

@XadillaX
Copy link
Collaborator

XadillaX commented Mar 6, 2015

@VaJoy 对 ES6 没了解。

@nunnly nunnly added 难度6 and removed 难度5 labels Mar 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants