Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

Is 'require(...)' a common javascript pattern or a library function?

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)?

e.g. var http = require('http') or var casper = require('casper').create()

I'm curious if this is a pattern that has become standardized for SSJS or does every library/tool call an existing function?

Please pardon the n00b dimension to the question but I would like to know the 'why' behind its omnipresence :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...