Javascript闭包 - 来自全局范围的重写函数的行为 [英] Javascript closures - behavior of overridden functions from the global scope

查看:126
本文介绍了Javascript闭包 - 来自全局范围的重写函数的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是更多的javascript原则。

  function done(){console.log done(){...`'); } 
var done = function(){console.log('done defined with`var done = ...`'); }
done = function(){console.log('without`var`,just`done = ...`'); }



如果在< script>



但是如果我把它们放在一个闭包(function(){ 函数定义 }())将覆盖全局定义的函数 done 或在其各自关闭内定义的任何其他 done()功能?



如果上述问题无意义,这里是改造;




  • 以下代码应该在任何JS运行时执行相同的操作?

  • eval -ing代码在上下文或全局范围内执行该特定代码?

  • 如何配置 setTimeout 调用,使其引号之间的代码执行到特定 setTimeout 已被调用(请参阅 > c>中的第二个超时)?我的意思是除了定义window.blabla函数并告诉他们在运行后删除自己之外,还有什么其他方法?

      function done d){console.log('cha cha cha:'+ d); } 
    setTimeout(function(){done(2);},3500);

    for(i = 0; i <10; i ++){
    (function(){
    done = function(x){console.log('done# i +'sais:'+ x);}
    setTimeout(function(){done(i * 2);},2500);
    setTimeout .toString()+'();',2500);
    }());
    }



解决方案

对于一般行为的初始问题:




  • var done = code>和 function done 做基础相同的事情。


  • done = 将在范围内设置相应的 done 变量,或者如果这样的变量不存在并且程序没有在严格模式下运行,则将创建一个全局变量。 p>


  • 在全局级别,在任何函数之外, var done = done = 应该工作相同,但如果你尝试使用另一个脚本标签中的变量,它们在IE中的工作方式不同(坚持 var =




至于非常邪恶的setTimeout和eval问题:




  • 是的,我想这种东西应该标准化,无处不在。我仍然会测试它。 (或者你可以使用一个不同的解决方案,考虑到evil eval是什么)


  • eval 黑魔法这样做)。如果要在全局范围上运行代码,可以改用 new Function


  • 要让settimeout在curent范围中运行字符串,您可以自己添加 eval

      var done = function(d){console.log('outer done',d); }; 

    (function(){
    var done = function(x){console.log('inner done',x);};

    setTimeout (){return($)$ {set(){return($)$ { done(3)');},600); // inner done
    }());


  • 再次,为什么要在setTimeouts中评估内容?这一切听起来都是极其邪恶的!



This question is more on javascript principle.

function done(){ console.log('done defined with `function done(){ ...`'); }
var done = function(){ console.log('done defined with `var done = ...`'); }
done = function(){ console.log('without `var`, just `done = ...`'); }

If defined right inside <script> tags, will they all do the same thing, right?

But if I place them in a closure (function(){ function definintion goes here }()) will any of these three types override either the globally defined function done() or any other done() functions that are defined inside their respective closures?

If the question above doesn't make sense, here's to rephrasing;

  • is the following code supposed to do the same thing in any JS runtime?
  • eval-ing code anywhere executes that particular code within the context or the global scope?
  • how can a setTimeout call be configured so that the code between its "quotes" executes to inside the scope where that particular setTimeout has been called (please see second timeout inside for below)? I mean is there any other way besides defining window.blabla functions and telling them to delete themselves after they run?

    function done(d){ console.log('cha cha cha: '+d); }
    setTimeout( function(){ done(2); }, 3500 );
    
    for(i=0; i<10; i++){
        (function(){
            done = function(x){ console.log('done #'+i+' sais: '+x); }
            setTimeout(function(){ done(i*2); },2500);
            setTimeout(function(){ done(i*2); }.toString()+'(); ',2500);
        }());
    }
    

解决方案

For the initial question on general behaviour:

  • var done = and function done do basicaly the same thing. They will shadow the outer definition in the inner scope but they will not replace it on the outer scope.

  • done = will set the corresponding done variable in scope or will create a global variable if such a variable does not exist and the program is not running in strict mode.

  • At a global level, outside any function, var done = and done = should work the same, but they work differently in IE if you try to use the variable in another script tag (stick to var = - its better anyway).

As for the very evil setTimeout and eval questions:

  • Yes, I guess this kind of stuff should be standardized enough to work the same everywhere. I would still test it anyway. (Or you could use a different solution, given how evil eval is)

  • eval runs code in the current scope (using deep black magic to do so). If you want to run code on the global scope you can use new Function instead.

  • In order to have the settimeout run the string in the curent scope you can add the eval yourself:

    var done = function(d){ console.log('outer done', d); };
    
    (function(){
        var done = function(x){ console.log('inner done', x); };
    
        setTimeout(function(){ done(1); }, 200);           //inner done
        setTimeout('done(2)', 400);                        //outer done
        setTimeout(function(){ eval('done(3)'); }, 600);   //inner done
    }());
    

  • Once again, why are you eval-ing things in the setTimeouts? This all sounds profoundly evil!

这篇关于Javascript闭包 - 来自全局范围的重写函数的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆