我的 html
页面中有两个 js
文件。如果第一个开始于:
(function($){
..
..
}(jQuery));
我可以将一个 var
插入到 function($,varname)
中,返回
它的值并在另一个文件中使用它吗?
最佳答案
为此你需要一个全局变量。您可以通过以下几种方式之一执行此操作。假设我们需要将值“Bacon”发送到其他脚本。
(function($){
window.myScriptsExports = "Bacon";
}(jQuery));
// OR
var myScriptsExports = (function($){
// other code
return "Bacon";
// NO other code
}(jQuery));
// OR (not really recommended)
(function($){
// other code
$.myScriptsExports = "Bacon";
// other code
}(jQuery));
关于javascript - 从js文件返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15201355/