在JavaScript中使用的'define'是什么(除了显而易见的)? [英] What is 'define' used for in JavaScript (aside from the obvious)?

查看:1087
本文介绍了在JavaScript中使用的'define'是什么(除了显而易见的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了关于此的文档的高低,但我在任何地方都找不到任何东西。

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

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

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.

他们的sidebar.js从这开始,但我不能为我的生活找到任何文档这解释了它的含义。

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

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

然后在该包装器中继续定义一堆函数,所以 vars 和一些 proptotypes - 我可以解决这个问题...

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

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

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

推荐答案

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

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


模块与传统脚本文件的不同之处在于
定义了具有良好范围的对象,可避免污染全局
命名空间。它可以显式地列出它的依赖关系并获得
那些依赖关系的句柄,而不需要引用全局对象,但
代替接收依赖关系作为
定义模块的函数的参数。

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.

define 的define with dependencies形式描述如下: / p>

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


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

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.

这篇关于在JavaScript中使用的'define'是什么(除了显而易见的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆