javascript RequireJS:“requirejs”和“require”函数之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13605600/
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-10-26 19:15:18  来源:igfitidea点击:

RequireJS: Difference between "requirejs" and "require" functions

javascriptrequirejsrequire

提问by Matthias Bayer

I am using requireJS 2.x. I found out that some tutorials (and the official docs) sometimes use

我正在使用 requireJS 2.x。我发现一些教程(和官方文档)有时会使用

requirejs.config({ [...] });
requirejs(["module"]) ...

and sometimes

有时

require.config({ [...] });
require(["module"]) ...

Is there any difference between those two functions (require and requirejs)? I could not find any word about that in the docs. :(

这两个函数(require 和 requirejs)之间有什么区别吗?我在文档中找不到任何关于它的词。:(

回答by Simon Smith

They are exactly the same.

它们完全一样。

The reason is some environments might already have a require, in which case RequireJS doesn't overwrite it and allows usage of the library through requirejs

原因是某些环境可能已经有一个require,在这种情况下 RequireJS 不会覆盖它并允许通过requirejs

See this commit - https://github.com/jrburke/requirejs/commit/be45948433b053921dc6a6a57bf06d04e13b3b39

看到这个提交 - https://github.com/jrburke/requirejs/commit/be45948433b053921dc6a6a57bf06d04e13b3b39

回答by Louis

Are requirejsAnd requirethe Same?

requirejsrequire一样吗?

As of RequireJS 2.1.15, requireand requirejsin the global space are indeed "exactly the same", as can be evidenced by this test you can perform in the console:

作为RequireJS 2.1.15的,requirerequirejs在全球空间确实是“如出一辙”,因为可以通过这个测试,你可以在控制台执行得到证实:

> require === requirejs
true

That the test returns truetells you they are the exact same function object. They are not two functions that happen to have similar or identical code. They are the same object, period.

测试返回true告诉您它们是完全相同的函数对象。它们不是碰巧具有相似或相同代码的两个函数。它们是同一个对象,句号。

Note, however when you execute define(['require'], function (require) {The requirepassed to the function is normally differentfrom the global require.

但请注意,当你执行define(['require'], function (require) {require传递给函数通常是不同的,从全局require

Should You Use requireor requirejs?

你应该使用require还是requirejs

It depends. RequireJS is an AMD loader but it is not the only loader in town. If you want to write code that conforms 100% to the AMD spec, so that someone using your code can use whatever loader they want without having to modify your code, then you should use requireat the global level, because requirejsis specific to RequireJS. Another AMD loader won't define it. The AMD spec defines requirebut not requirejs.

这取决于。RequireJS 是一个 AMD 加载器,但它不是镇上唯一的加载器。如果您想编写 100% 符合AMD 规范的代码,以便使用您的代码的人可以使用他们想要的任何加载器而无需修改您的代码,那么您应该require在全局级别使用,因为requirejs特定于 RequireJS。另一个 AMD 加载器不会定义它。AMD的规范定义require,但没有requirejs

If you are loading something else that defines a global requirethen you have to use requirejsat the global level to avoid conflict.

如果您正在加载定义全局的其他内容,require那么您必须requirejs在全局级别使用以避免冲突。

Inside a module, always use defineto obtain a reference to require. You should do this quite irrespective of whether there is a conflict in the global space.

在模块内部,始终使用define获取对 的引用require。无论全局空间是否存在冲突,您都应该这样做。

回答by Jazimov

OK, they may indeed be "exactly the same". Let's then focus on why you would use one versus the other...

好吧,它们可能确实“完全相同”。然后让我们关注为什么你会使用一个而不是另一个......

What is unclear is what should be considered "best practice": If requirejs provides extra assurance "if some environments might already have a require", then wouldn't it be a good idea to always use the requirejs function to define a require configuration rather than the require function?

不清楚的是什么应该被认为是“最佳实践”:如果 requirejs 提供了额外的保证“如果某些环境可能已经有一个需求”,那么总是使用 requirejs 函数来定义一个 require 配置不是一个好主意吗?比 require 函数?

Also, what happens if the unthinkable happens and the environment in question not only already has a "require" defined but also has a "requirejs" defined? Does that mean we should have a requirejsjs too? And so on...?

另外,如果发生了不可思议的事情,并且所讨论的环境不仅已经定义了“require”而且还定义了“requirejs”,会发生什么?这是否意味着我们也应该有一个 requirejsjs?等等...?

回答by JeevanReddy Avanaganti

They are Same Open website which loaded require already

它们是已经加载需要的相同打开的网站

then open Chrome console

然后打开 Chrome 控制台

type require in console and press enter

在控制台中输入 require 并按 Enter

type requirejs in console and press enter

在控制台中输入 requirejs 并按回车键

you can find that they are same function with different names

你会发现它们是相同的功能,但名称不同