Eval函数我们还特别熟稔,可是global伊娃(Eva)l方法也十分少动,大多数参考手册也无相关api,下边就是对准这用法及源码相应介绍:

 

jQuery.globalEval()函数用于全局性地举行同样截JavaScript代码。

示例:

var name = "全局变量";

function test(){
    var name = "局部变量";

    alert(name);                     // 局部变量

    eval( "alert(name);" );          // 局部变量

    $.globalEval( "alert(name);" );  // 全局变量
}

test();

 

可以看该办法以及eval方法相相比来一个功用域的限定不同异即始终处于大局功效域上边,下面举办源码分析:

// Evaluates a script in a global context
// Workarounds based on findings by Jim Driscoll
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
globalEval: function( data ) {
    if ( data && rnotwhite.test( data ) ) {
         // We use execScript on Internet Explorer
         // We use an anonymous function so that context is window
         // rather than jQuery in Firefox
            ( window.execScript || function( data ) {
                window[ "eval" ].call( window, data );
            } )( data );
     }
 },

 

注里的意思是关系了之办法的落实是以JimDriscoll的功底之上的同时将有关的章链接还把了点,索性链接还得打开瞅瞅,里面盖介绍了怎么被js代码在全局执行的计,对于ie浏览器而言则提供了一个无太常用之艺术(反正我是首先次于听到)—window.execScript

window.execScript
方法会按照提供的脚本语言执行同一段脚本代码。window.execScript
方法有半点独参数,第一个参数指定给实践的剧本代码段,第二单参数指定脚本代码语言类别(缺省值为 JScript),execScript
方法执行后的代码位于全局意图域下。举例:

 var str='全局'; 
    (function(){
        var str='局部';
        window.execScript('alert(str)');  //ie 全局
}());  

 

不过这么些方法现在只有ie帮忙,早期的chrome版本为协理现在已吐弃,具体的斯道介绍请参考http://ued.sina.com/?p=789;

开拓注释中之网址是一个英文网站四层爱尔兰语水品的自身果断的接纳了Google网页翻译效用,可以Google不给力告诉自己一筹莫展翻译,这尚未道只有会硬在头皮读下去,怕译错把原文附上请以原文为仍

For more standards-respecting browsers,
the way to do this should be to use the call function, which is a
standard function attached to every Function object.
So, eval.call(window, src) should work. But to understand why, it’s
important to know about context, in addition to scope. Every function
call has it’s own context: this is the object that’s represented by the
special value this. When we use the call function, the first parameter
is the context object we’ll use for this. This is handy for all kinds
of purposes, but for us, it’s just nice to use to set the context to
the window object – which, you’ll recall, is the global.

 

即刻段是供解决格局的,对于正规浏览器而言能够应用eval函数解析js代码字符串,然后经过对象冒充的办法拿效用域指向window,下面就是是协调之测试:

var str='全局'; 
(function(){
     var str='局部';
     eval.call(window, "alert(str)"); 
}());  

经过测试ie9及以上以及非ie均弹来“全局“
,ie9以下弹出有,表达那么些点子发生见兼容性有题目啊,可是这主意自然就于非IE用的,你ie爱咋咋地吧

Sadly, eval.call(window,src) breaks on
Chrome – it complains about contexts not matching. Odd – and I was
unable to Google up why this might be so. But a couple lucky guesses
later, and I discovered that window.eval.call(window,src) works on all
non-IE browsers. Now, when I say “var j = 1”, the window[j] is the
variable that’s set… So, that’s good. Why do we have to add the
extra window. on Chrome? Not sure – I could guess, but it’s too likely
to be wrong.

 

该文作者用了数为此了”odd“,我也以为分外”odd”,作者说在Google中报错了,然后把代码修改也window.eval.call(window,src)就得啊,正而作者所困惑的,全局方法是勿需要加window直接调用的这我们都晓得,可是本人做检测的时是从未有过起问题之,应该是经久不衰了Google已经解决了,最终作者还关系火狐在推行解析this存在问题

 

At this point, I thought
we’d licked the problem. No such luck. Sure, global variables are
getting set, but it turns out that if you say: alert(this) – then you
would correctly receive the global object back on Chrome and Safari, but
not Firefox – there, you’d get back the object that was the enclosing
object before the call function got called. Very odd, and likely a bug
in their
implementation.

 

既是全局解析alert(this)肯定是window才对,而火狐弹出多年来之功效链的目的,对于需要测试下,代码如下:

 var str='全局'; 
      (function(){
           var str='局部';
           window.eval.call(window, "alert(this)"); 
  }());  

透过检测火狐没有问题,估量火狐也是早把这题目处理了,在篇章的最后作者留了一个智

var globalEval = function globalEval(src) {
    if (window.execScript) {
        window.execScript(src);
        return;
    }
    var fn = function() {
        window.eval.call(window,src);
    };
    fn();
};

足说这些办法都充裕了,不过回过头来看看我们的jQuery源码会发现精简了好多

data && rnotwhite.test( data )

欠法确保有多少而是无带来空格的多寡,当然你啊得不传字符串然则没关系意义rnotwhite的定义在构造函数中

// Check if a string has a non-whitespace character in it
    rnotwhite = /\S/,

有关为何而检测不可知发空格或登时首外文中有外开发者提出了如是空白的当ie8出现谬误

 

by vesperaba - 2012-10-18 08:54
If src is a blank string you will get an error in IE8. To avoid that I added the following check at the beginning:

if (src == undefined || src == '') {
     return;
}

//The whole function will be:
var globalEval = function globalEval(src) {
if (src == undefined || src == '') {
     return;
}
if (window.execScript) {
    window.execScript(src);
       return;
   }
   var fn = function() {
       window.eval.call(window,src);
   };
   fn();
};

 

经过逻辑或运算来拔取何种模式分析,假设是利用eval则是地处一个打进行的匿名函数中,结构相当简单

( window.execScript || function( data ) {
      window[ "eval" ].call( window, data );
 } )( data );

另需留意的事该法没有回值,或者可以精通也重回undefined

 

相关文章

网站地图xml地图