JS in the exports detailed

Source: Internet
Author: User
This article is mainly to share with you JS exports detailed, mainly in the way of code and we share, hope to help everyone.

Notation 1exports.hello = function () {    console.log (' World ');} notation 2var f = {    hello:function () {        console.log (' World ');    }} Module.exports = f;

Suppose we write this module with the file name Hello.js, execute the following code
var h = require (' hello ');
H.hello ();

For both of these, the result of executing this code is the same.

Module.exports:

Give me a chestnut:

1.

A.jsmodule.exports = [' aaa ', 18]//b.jsvar a= require (' a ') Console.log (a[1])//output 182,//a.jsmodule.exports =function () {this.show=function () {console.log (' hahah~ ');}}  B.jsvar a= require (' a '); var obj = new A (); obj. Show ();//Output Hahah~module.exports My understanding is: you give something to Module.exports,require after you will get something exports://a.jsexports.show =function () {console.log (' hahah~ ');} B.jsvar a= require (' a '); A.show ();//output Hahah~exports is already an object, you can add properties to this object, and after require you get the exports object. But you can't assign a new object to exports, like exports={} It is also important to note that if Module.exports already has the content, then exports all operations will be invalidated, remember again what Prototype,prototype is to do, it is to add attributes in the prototype, the prototype is like the parent class in C + +, I'll give you another chestnut. 1,//a.jsmodule.exports =function () {}module.exports.prototype.show = function () {console.log (' hahah~ ');} B.jsvar a= require (' a '); var obj = new A () obj.show ()//output hahah~ Finally, say the class method, speaking of class, that must be module.exports. Chestnuts in this 1,//a.jsmodule.exports =function () {}module.exports.show = function () {console.log (' hahah~ ');} B.jsvar a= require (' a '); A.show ()//Output hahah~# #module. The difference between exports and exports each node. js execution file automatically creates a module object that, the module object creates a property called exports, and the initialized value is {}module.exports = {}; node. js for easy export of function functions, node. JS automatically implements the following statement FOO.JSEXPORTS.A = function () {Console.log (' a ')}EXPORTS.A = 1 Test.jsvar x = Require ('./foo '); Console.log (X.A) see here, I believe we all see the answer, exports is the value of reference module.exports. When the module.exports is changed, exports will not be changed, and when the module is exported, the actual export execution is module.exports, instead of exports see the following example FOO.JSEXPORTS.A = function () { Console.log (' a ')}module.exports = {A:2}EXPORTS.A = 1 Test.jsvar x = require ('./foo '); Console.log (X.A) result : 2exports expires after Module.exports is changed. is not beginning a bit of a cheerful, the following will be listed in the Open source module, often see a few ways to use. # #module. Exports = viewfunction View (name, options) {options = Options | |  {};  THIS.name = name;  This.root = Options.root;  var engines = Options.engines;  This.defaultengine = Options.defaultengine;  var ext = This.ext = extname (name); if (!ext &&!this.defaultengine) throw new Error (' No default engine was specified and no extension is pro  vided. '); if (!ext) name + = (ext = This.ext = ('. '!! = this.defaultengine[0]? '. ': ') + this. Defaultengine); This.engine = Engines[ext] | |  (Engines[ext] = require (Ext.slice (1)). __express); This.path = this.lookup (name);} Module.exports = View;javascript inside there is a sentence, function is object, View is an object, Module.export =view, that is equivalent to export the entire View object. When the outside module calls it, it can invoke all of the view's methods. It is important to note, however, that only the static method of the view can be called, and the method created by prototype is a private method of view. Foo.jsfunction View () {}view.prototype.test = function () {console.log (' test ')}view.test1 = function () {Console.log (' Test1 ')}module.exports = Viewtest.jsvar x = require ('./foo '); Console.log (x)//{[Function:view] test1: [Function]}conso Le.log (x.test)//undefinedconsole.log (x.test1)//[function]x.test1 ()//test1# #var app = exports = Module.exports = {}; , when we understand the principle, it is not difficult to understand that the wording is a bit redundant, in fact, to ensure that the initialization of the module environment is clean. It is also convenient for us, even if we change the object that Module.exports points to, we can still follow the exports features exports = Module.exports = createapplication;/** * Expose mime. */exports.mime = Connect.mime; example, where module.exports = CreateApplication changed module.exports, let exports fail, through exports = Module.exports method to restore its original characteristics. # #exports. init= function () {} This is the simplest, direct exportThe method of the module INIT. # #var Mongoose = Module.exports = exports = new Mongoose; The set is all-in-one, but according to the above, you should not get the answer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.