CommonJS specification (rpm)

 

Outline

CommonJS is a specification of the server module, Node.js adopted this specification.

The CommonJS specification, a module is a separate file. Load modules require the use of a method of reading a document and perform, and returns inside the exports file objects. Here is a simple module file example.js.

console.log("evaluating example.js");
var invisible = function () {
  console.log("invisible");
}
exports.message = "hi";
exports.say = function () {
  console.log(message);
}

Require the use of methods, load example.js.

var example = require('./example.js');

In this case, it corresponds to the variable example exports object in the module, so they can pass this variable, the method using the respective module.

{
  message: "hi",
  say: [Function]
}

The default method require reading js file js suffix may be omitted.

var example = require('./example');

js file name the need to add the path, it can be relative (with respect to the method require the use of a file), or an absolute path. If you omit the path, node.js would think that you want to load a kernel module, or a module in the local node_modules directory has been installed. If the load is a directory, node.js will first look for package.json file in that directory, the file is loaded module main attributes mentioned, or to look for index.js files in that directory. Look a little example of a complex.

// foobar.js
function foobar(){
        this.foo = function(){
                console.log('Hello foo');
        }

        this.bar = function(){
                console.log('Hello bar');
        }
} 
exports.foobar = foobar;

The module is invoked as follows:

var foobar = require('./foobar').foobar, test = new foobar(); 
test.bar(); // 'Hello bar'

Sometimes, no exports returns an object, only it returns a function. At this time, it must be written module.exports.

module.exports = function () {
  console.log("hello world")
}

Compatibility AMD CommonJS norms and specifications

CommonJS specifications load module is synchronous, meaning that only the loading is completed, to perform subsequent operations. AMD specification is Non-synchronized loading module, allowing to specify a callback function. Because Node.js is mainly used for server programming, module files are generally already exists in the local hard disk, so loading up more quickly, regardless of the way asynchronous loading, so CommonJS more applicable norms. However, if a browser environment, from server-side load module, then you must use asynchronous mode, the browser generally use the AMD specification.

AMD specifications define methods using module definitions, the following is an example:

define(['package/lib'], function(lib){ 
    function foo(){
        lib.log('hello world!');
    }  
    return {
        foo: foo
    };
});

AMD specification allows CommonJS compatible module output specification to be written in this case define the following methods:

define(function (require, exports, module){
    var someModule = require("someModule");
    var anotherModule = require("anotherModule");    

    someModule.doTehAwesome();
    anotherModule.doMoarAwesome();

    exports.asplode = function (){
        someModule.doTehAwesome();
        anotherModule.doMoarAwesome();
    };
});

Reference links

Addy Osmani, Writing Modular JavaScript With AMD, CommonJS & ES Harmony

Reprinted from: http: //javascript.ruanyifeng.com/nodejs/commonjs.html#toc0

 

Reproduced in: https: //www.cnblogs.com/JoannaQ/p/3756295.html

Guess you like

Origin blog.csdn.net/weixin_33722405/article/details/93057382
rpm