gpt4 book ai didi

javascript - 简化的 commonJS 包装器如何在 require.js 中工作?

转载 作者:行者123 更新时间:2023-11-30 12:07:52 25 4
gpt4 key购买 nike

考虑他们网站上的这个例子

define(function (require) {
var foo = require('foo');

//Define this module as exporting a function
return function () {
foo.doSomething();
};
});

我的问题是,由于“foo”是异步加载的,它下面的 Javascript 如何在加载之前不执行?

最佳答案

这在 http://requirejs.org/docs/api.html#cjsmodule 中有解释和 http://requirejs.org/docs/whyamd.html#sugar .

Require.js 会在某个时候(在运行函数之前)查看函数的字符串表示,找到所有require 调用以确定依赖项并加载它们.

To make this easier, and to make it easy to do a simple wrapping around CommonJS modules, this form of define is supported, sometimes referred to as "simplified CommonJS wrapping":

define(function (require) {
var dependency1 = require('dependency1'),
dependency2 = require('dependency2');
return function () {};
});

The AMD loader will parse out the require('') calls by using Function.prototype.toString(), then internally convert the above define call into this:

define(['require', 'dependency1', 'dependency2'], function (require) {
var dependency1 = require('dependency1'),
dependency2 = require('dependency2');
return function () {};
});

This allows the loader to load dependency1 and dependency2 asynchronously, execute those dependencies, then execute this function.

关于javascript - 简化的 commonJS 包装器如何在 require.js 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34661039/

25 4 0
文章推荐: javascript - 如何使用正确的值在 plotly.js 中制作堆积面积图?
文章推荐: javascript - 谷歌地图生成空白灰色
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com