$.getScript return 是什么,它对作用域有什么作用?

What does $.getScript return, and what does it do to scope?

我正在创建一个插件,我想在其中加载一些脚本并为每个脚本 运行 函数 plugin.

我创建了一个 set of tests/examples(代码如下)。

问题:

  1. AJAX 传递通常的 data, textStatus, jqxhr 参数集。但显然也创建了一个 plugin 函数可用的范围。在文档中找不到任何关于此的内容。更多details/explanation请!

  2. 似乎在范围内的 this 到底是什么?

  3. 我 运行 通过映射脚本名称列表获取脚本的第三个示例按预期工作。

  4. 构建延迟列表然后 运行 when 行为很奇怪。我没有得到函数有 运行(无输出)的指示,当我删除延迟时,它似乎总是先完成("done" 打印在其他所有内容之前)。函数是 运行ning 吗?我尝试添加一个 alert,但当我使用 when.

  5. 时它没有出现

index.js

var script_names = ["one.js", "two.js", "three.js"];

function as_callback(script_name)
{
  console.log("plugin function run as callback");
  console.log(`$.getScript(${script_name}, (data, textStatus, jqxhr) => plugin());`);
  $.getScript(script_name, (data, textStatus, jqxhr) => plugin());
  console.log();
}

function from_this(script_name)
{
  console.log("plugin function referred to from 'this'");
  console.log(`$.getScript(${script_name}, (data, textStatus, jqxhr) => this.plugin());`);
  $.getScript(script_name, (data, textStatus, jqxhr) => this.plugin());
  console.log();
}

function with_map(script_names)
{
  console.log("with map");
  console.log("string_names: " + JSON.stringify(script_names));
  console.log(`
  script_names.map((x) => 
  {
    $.getScript(x, (data, textStatus, jqxhr) => plugin())
  });
  `);
  script_names.map((x) => 
  {
    $.getScript(x, (data, textStatus, jqxhr) => plugin())
  });
  console.log();
}

function with_push_and_when(script_names)
{
  console.log("build array of deferred and run with when");
  console.log(`
  var plugs = [];
  script_names.map(x => $.getScript(x, (data, textStatus, jqxhr) => plugs.push(plugin)));
  $.when(plugs).done(console.log("done"));
  `);
  var plugs = [];
  script_names.map(x => $.getScript(x, (data, textStatus, jqxhr) => plugs.push(plugin)));
  $.when(plugs).done(console.log("done"));
  console.log();
}

as_callback('one.js');

setTimeout("from_this('two.js')", 2000);

setTimeout("with_map(script_names)", 4000);

setTimeout("with_push_and_when(script_names)", 6000);

var plugs = [];
script_names.map(x => $.getScript(x, (data, textStatus, jqxhr) => plugs.push(plugin)));
setTimeout("console.log('run when in global scope');$.when(plugs).done(console.log('done'))", 8000);

one.js

var plugin = function()
{
  console.log("one.js\n\n");
  // alert("one");
  return "one";
}

two.js

var plugin = function()
{
  console.log("two.js\n\n");
  return "two";
}

three.js

var plugin = function()
{
  console.log("three.js\n\n");
  return "three";
}

输出

plugin function run as callback
$.getScript(one.js, (data, textStatus, jqxhr) => plugin());

one.js


plugin function referred to from 'this'
$.getScript(two.js, (data, textStatus, jqxhr) => this.plugin());

two.js


with map
string_names: ["one.js","two.js","three.js"]

  script_names.map((x) => 
  {
    $.getScript(x, (data, textStatus, jqxhr) => plugin())
  });


two.js


three.js


one.js


build array of deferred and run with when

  var plugs = [];
  script_names.map(x => $.getScript(x, (data, textStatus, jqxhr) => plugs.push(plugin)));
  $.when(plugs).done(console.log("done"));

done

run when in global scope
done

注意: 我将接受的答案添加到 repl.it.

  1. 回调函数在加载脚本后在全局上下文中运行。由于脚本定义了全局变量plugin,可以从回调函数中访问

  2. $.getScript 没有设置特定的上下文,所以 this 将是全局 window 对象。 this.pluginwindow.plugin一样,都是全局变量。

  3. 没错。

  4. $.getScript returns 一个承诺,但你并没有把它们推到 plugs,你只是在推 plugin

.map() 的结果赋给 plugs 以获得正确的承诺数组。

var plugs = script_names.map(x => $.getScript(x, (data, textStatus, jqxhr) => plugin()));
$.when(plugs).done(console.log("done"));