2017-03-04 84 views
0

当函数刚好在调用下面定义时,我得到一个Uncaught ReferenceError: fun is not definedjQuery未捕获的引用错误

我简化尽可能我的脚本重现错误

fun(); 

function fun() { 
} 

var promise = new Promise((resolve, reject) => call(resolve, reject)); 

const call = (success, error) => { }; 

我得到这个例外

Uncaught ReferenceError: fun is not defined 
    at eval (eval at globalEval (jquery.js:545), <anonymous>:1:1) 
    at eval (<anonymous>) 
    at Function.globalEval (jquery.js:545) 
    at text script (jquery.js:7617) 
    at ajaxConvert (jquery.js:7592) 
    at done (jquery.js:7350) 
    at XMLHttpRequest.<anonymous> (jquery.js:7822) 
    at Object.send (jquery.js:7845) 
    at Function.ajax (jquery.js:7301) 
    at Function._evalUrl (jquery.js:5880) 

这似乎是工作,如果我删除var promise线或{}更换call(resolve, reject) ,但我不明白为什么。

jQuery version 2.0.3

任何想法为什么会发生这种情况?

编辑:我有其他问题与jQuery的globalEval有关,我通过移动jQuery脚本标记作为最后一个解决了它们,但仍然没有帮助。

+0

我不明白...你在哪里叫'fun()'?代码的重要部分在这里丢失。我无法复制你的问题。请添加复制您在这里或http://jsfiddle.net上遇到的问题的代码。但是这个错误本身就说明了一切。这可能是一个范围相关的问题。 – Ionut

+0

@Ionut哎呀,这溜出了。只是缺少顶部的乐趣() – user4388177

+0

我没有看到这里jQuery如何相关。你在哪里使用它? – Li357

回答

0
<head> 
    <script src="Scripts/jquery/jquery.js"></script> 

    <script> 
     function run() { 
      var toReload = "app.js"; 
      $('script[src="' + toReload + '"]').remove(); 
      $('<script>').attr('src', toReload).appendTo('head'); 
     } 
    </script> 
</head> 
<body> 
    <div id="content"> 
     <button onclick="run()">Run</button> 
    </div> 
</body> 

我是动态加载我的代码,jQuery变得困惑。在jQuery解决它之前将脚本移动到脚本标记中。