JavaScript 中的“定义”是什么(除了显而易见的)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10331305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:54:23  来源:igfitidea点击:

What is 'define' used for in JavaScript (aside from the obvious)?

javascriptrequirejsrequirejs-define

提问by David O'Sullivan

I have searched high and low for documentation on this, but I just cannot find anything anywhere.

我已经搜索了很多关于这方面的文档,但我在任何地方都找不到任何东西。

I am using Aloha and want to use their sidebar prototype to create a new side bar of my own attached to other plugin functionality.

我正在使用 Aloha 并想使用他们的侧边栏原型来创建一个我自己的新侧边栏,附加到其他插件功能。

Their sidebar.js starts off with this, but I can't for the life of me find any documentation that explains what it means.

他们的 sidebar.js 从这个开始,但我一生都找不到任何解释它含义的文档。

define( [
    'aloha/core',
    'aloha/jquery',
    'aloha/selection'
], function (Aloha, jQuery, Selection, Plugin) {

It then goes on in that wrapper to define a bunch of functions, so varsand some proptotypes- which I can just about get my head around...

然后它在那个包装器中继续定义一堆函数,所以vars和一些proptotypes- 我几乎可以理解......

What is that saying or where can I find an explanation?

那是什么意思或我在哪里可以找到解释?

回答by James Allardice

I can't say for sure without seeing the entire script, but it's likely to be the definefunction from RequireJS, in particular the "define with dependencies" form of that function. It is used to define a "module":

如果没有看到整个脚本,我不能肯定地说,但它很可能是defineRequireJS函数,特别是该函数的“定义依赖项”形式。它用于定义一个“模块”:

A module is different from a traditional script file in that it defines a well-scoped object that avoids polluting the global namespace. It can explicitly list its dependencies and get a handle on those dependencies without needing to refer to global objects, but instead receive the dependencies as arguments to the function that defines the module.

模块不同于传统的脚本文件,它定义了一个范围良好的对象,避免污染全局命名空间。它可以显式地列出其依赖项并获取这些依赖项的句柄,而无需引用全局对象,而是将依赖项作为定义模块的函数的参数接收。

And the "define with dependencies" form of defineis described as follows:

并且“使用依赖项定义”形式的define描述如下:

If the module has dependencies, the first argument should be an array of dependency names, and the second argument should be a definition function. The function will be called to define the module once all dependencies have loaded. The function should return an object that defines the module.

如果模块有依赖,第一个参数应该是一个依赖名称数组,第二个参数应该是一个定义函数。一旦加载了所有依赖项,将调用该函数来定义模块。该函数应该返回一个定义模块的对象。

回答by TheeBen

This is AMD pattern for writing modules which AMD stands for Asynchronous Module Definition for when you need to import modules async basically rather than something like commonJS.

这是用于编写模块的 AMD 模式,AMD 代表异步模块定义,用于基本上异步导入模块而不是像 commonJS 这样的模块。

define(['module1', 'module2'], function(module1, module2) {
  console.log(module1.sayHi());
});

Define takes an array of dependencies and once all those are loaded in the background (async) in a non-blocking way, define calls the callback which in turn accepts arguments (in this case the dependencies).

定义需要一个依赖项数组,一旦所有这些都以非阻塞方式在后台(异步)加载,定义调用回调,后者又接受参数(在本例中为依赖项)。

Another thing to note is that each one of those modules also needs to be defined using "define" keyword. So for instance module1 would be defined like below :

另一件需要注意的是,这些模块中的每一个都需要使用“define”关键字来定义。因此,例如 module1 将被定义如下:

define([], function() {

  return {
    sayHi: function() {
      console.log('Hi Hi');
    },
  };
});

This way of writing modules (AMD) allows you to write with browser compatibility in mind (no require() like in nodeJS) and also you can define many formats including objects, JSON, etc while for instance commonJS needs modules to be objects.

这种编写模块的方式 (AMD) 允许您在编写时考虑到浏览器兼容性(不像 nodeJS 中的 require() ),并且您还可以定义多种格式,包括对象、JSON 等,而例如 commonJS 需要模块是对象。

Keep in mind, AMD has it's own downfalls. Hope this helps someone.

请记住,AMD 有其自身的缺点。希望这可以帮助某人。