“严格使用”; + jQuery.getScript()=脚本不能导出到全局名称空间 [英] "use strict"; + jQuery.getScript() = script can't export to global namespace

查看:139
本文介绍了“严格使用”; + jQuery.getScript()=脚本不能导出到全局名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下脚本,名为 include_strict.js 。执行后,我应该定义 window.global1

 use严格; 

var globalVar = {};
alert(typeof window.globalVar);

但是,如果我将它从一个JavaScript块中加入

  $。getScript(include_strict.js); 

警报说未定义。为什么?这里发生了什么?



仅供参考,如果我使用脚本标记包含文件,那不会发生什么情况:

 < script type =text / javascriptsrc =include_strict.js>< / script> 

在这里,我看到预期的警报, object 。如果我删除use strict; ,那么 jQuery.getScript() < script>; 与显示 object



已创建示例( https://docs.google.com/file/d/ 0B-XXu97sL1Ckb0x0OHptTmVMY00 / edit )来证明这一点。 使用 $。getScript (),它使用 eval 来执行无法在严格模式下修改全局范围的脚本:


其次,严格模式代码的 eval 不会将新变量引入周围范围。在普通代码 eval(var x;)中,将变量x引入周围函数或全局范围。这意味着,通常情况下,在包含对eval的调用的函数中,每个不涉及参数或局部变量的名称必须映射到运行时的特定定义(因为该eval可能引入了一个新变量,该变量会隐藏外部变量)。在严格模式下,eval仅为被评估的代码创建变量,所以eval不会影响名称是指外部变量还是某个局部变量:

来源: https://developer.mozilla.org/en-US / docs / JavaScript / Reference / Functions_and_function_scope / Strict_mode



解决方案不使用jQuery加载脚本,而是添加脚本元素添加到DOM。请注意,你甚至不能使用jQuery来追加元素;它会自动使用 $。getScript()


Suppose I have the following script, called include_strict.js. After it executes I should have window.global1 defined:

"use strict";

var globalVar = {};
alert(typeof window.globalVar);

But if I include it from a block of javascript with

$.getScript("include_strict.js");

The alert says undefined. Why? What is going on here?

FYI, that's not what happens if I include the file using a script tag:

<script type="text/javascript" src="include_strict.js"></script>

Here, I see the expected alert, object. And if I remove "use strict";, then both jQuery.getScript() and <script>; have the same effect of displaying object.

I've created an example (https://docs.google.com/file/d/0B-XXu97sL1Ckb0x0OHptTmVMY00/edit) to demonstrate this.

解决方案

It uses $.getScript() which uses eval to execute the script which cannot modify the global scope in strict mode:

Second, eval of strict mode code does not introduce new variables into the surrounding scope. In normal code eval("var x;") introduces a variable x into the surrounding function or the global scope. This means that, in general, in a function containing a call to eval every name not referring to an argument or local variable must be mapped to a particular definition at runtime (because that eval might have introduced a new variable that would hide the outer variable). In strict mode eval creates variables only for the code being evaluated, so eval can't affect whether a name refers to an outer variable or some local variable:

Source: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/Strict_mode

The solution is not using jQuery to load the script but appending a script element to the DOM. Note that you cannot even use jQuery to append the element; it will automatically use $.getScript() for it.

这篇关于“严格使用”; + jQuery.getScript()=脚本不能导出到全局名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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