'require(...)'是一个常见的javascript模式还是库函数? [英] Is 'require(...)' a common javascript pattern or a library function?
问题描述
我通常认为这是node.js脚本/模块中的第一行以及phantomJS,casperJS等。我很好奇,如果这是服务器端javascript(SSJS)的常见模式(类似于 #include
在C / C ++中或 import
在Java中)或者它是一个像RequireJS或LabJS这样的库被调用包含(目前我还没有机会在实践中使用)?
I usually find this as the first line in node.js scripts/modules as well as phantomJS, casperJS etc. I'm curious, if this is a common pattern for server-side javascript (SSJS) (similar to #include
in C/C++ or import
in Java) or is it a a library like RequireJS or LabJS that is being called for this inclusion (neither of which I have gotten a chance to use in practice, as yet)?
例如 var http = require('http')
或 var casper = require('casper')。create()
我很好奇这个模式是否已成为SSJS的标准化或每个库/工具是否调用现有函数?
I'm curious if this is a pattern that has become standardized for SSJS or does every library/tool call an existing function?
请原谅问题的 n00b 维度,但我想知道其无所不在的'原因':)
Please pardon the n00b dimension to the question but I would like to know the 'why' behind its omnipresence :)
推荐答案
require()
idiom是一个名为 CommonJS 。具体来说,该规范的那部分称为模块。 RequireJS 只是CommonJS的一个实现(它通常是一个浏览器端实现 - 事实上,它需要不同的方法,因为浏览器的异步性质。)
The require()
idiom is part of a specification known as CommonJS. Specifically, that part of the spec is called 'Modules'. RequireJS is just one implementation of CommonJS (and it's usually a browser-side implementation - in fact, it takes a different approach because of the asynchronous nature of the browser).
如果您查看CommonJS网站上的实施列表,您将查看节点.js列出。请注意,它实现了模块。因此,它就是它的来源 - 它非常内置。
If you look at the list of implementations on the CommonJS site, you'll see Node.js listed. Notice that it implements 'Modules'. Hence, that's where it's coming from - it's very much built-in.
这篇关于'require(...)'是一个常见的javascript模式还是库函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!