In order to allow the same module to run on the front and back end, it is necessary to consider the compatibility front end and the module specification environment in the writing process. In order to maintain the consistency of the front and back end, class library developers need to wrap the class library code inside a closure. The following code shows how the Hello () method can be defined in a different run environment, compatible with node (CommonJS
COOMONJS modules provide a clean syntax for importing dependencies. This lesson would take a look at the basics of using CommonJS modules.App.jsvar dep = require ('./DEP'// exports a string backDep.jsModule.exports = "exports a stringback";You can exports anything, such as a function:App.jsvar dep = require ('./DEP'// Export a function backDep.jsfunction () { return "exports a function back";}Exprots multi-value:App.jsvar dep = require ('./dep '
The module loading mechanism is known as the COMMONJS specification. Under this specification, each .js file is a module that does not conflict with the variable name and function names used internally, for example, hello.js and main.js both declare global variables var s = ‘xxx‘ , but do not affect each other.A module wants to expose a variable (function is also a variable) module.exports = variable; , you can use, a module to refer to other module
,This point undefined}CommonJS {Module.exports = ...: ' Can only output one, and the back will cover the above ',Exports. ...: ' Can output multiple ',The runtime determines the interface and the runtime loads the module,The module is an object, the object is loaded,The entire module is loaded, all the interfaces are loaded in,The output is a copy of the value, that is, the value change in the original module does not affect the value that has already
Index9.htmlOne of the loaders here implements the COMMONJS specification, which is exactly the same as the Nodejs module loaderModule definitionExports.txt = "Hello word"; module.exports={ "name": "Zhang San"}Require loading this require ("./xxx.html!text");Require loading CSS require ("./xxx.css!css");Default loading JS, and loading JS can omit the suffix nameThere are no rules for COMMONJS to be found
ObjectiveMany years ago, we introduced code that was introduced directly with script SRC, or wrote a loadscript on its own.With script Src Way, when the project JS file more, there will be a lot of script tags on the page, put aside performance for the time being, the code is very ugly, and very poor maintenance, especially when the JS file has a dependency, more difficult to deal with.In order to solve this pain point, to handle the load and dependency of JS, and to promote JS modular developme
CommonJS, CMD, AMD, and NodeJS create the future of JavaScript. commonjsnodejs
CMDIt is developed by Yubo Dashen in China.SeaJSWhich belongsCommonJSIn additionAMDThe framework isRequireJS
1. Both are an implementation of Asynchronuous Module Definition;
2,CMDAndAMDAllCommonJSIs a standard implementation Definition,RequireJSAndSeaJSIs the corresponding practice;
3,CMDAndAMDDifferences:CMDIt is equivalent to loading on demand. when defining a module, y
In addition to providing the AMD module interface, CMD module interface, but also to provide the native JS interface.Because CMD and AMD both can be used return to define the external interface, it can be combined into a code.A code that can be used directly is as Follows:;(function){functionMyModule () {// ... }var modulename = mymodule;if (typeofmodule!==' Undefined ' typeof exports = = =' Object ') {module.exports = modulename;} else if (typeof define = = = ' function ' (define.amd | | DEFI
requireWhen a parameter string is found to point to a directory, the file for that directory is automatically viewed package.json , and then main the portal file specified by the field is loaded. If the package.json file does not have a main field, or there is no package.json file at all, the file or file under that directory is loaded index.js index.node .Reprint: http://javascript.ruanyifeng.com/nodejs/module.html#toc4Record webpack commonJs load fi
Recently, a designer was developed as a prototype system of the company,
Purpose:
1. Drag the HTML control to quickly generate a page and automatically generate the page [JS, HTML], non-system
2. Robust Exception Handling Mechanism. The system is compatible with sales personnel and customers. It is a designer but very simple and practical.
Keep it simple, stupid
Target-oriented:
The sales staff can quickly create a page based on the customer's changing needs, instantly modify and present
parallel, loads all dependent modules, but does not immediately execute the module until it is really needed (require) to begin parsing,Seajs is the correct way to load modules asynchronously, but the order in which the modules are executed is strictly in the order in which they appear in the code (require), so it is more logical. What do you say, Requirejs?This is about executing (actually running the code in the Define) module, not the load (load file) module.Module loading is parallel, no di
requireThe command is used to load the file, and the suffix name defaults to .js .var foo = require (' foo ');// equivalent to var foo = require (' foo.js ');Depending on the format of the parameters, the require command goes to different paths to
At the end of July 2014, another meeting of TC39 finalized some of the last details about the ECMAScript 6 modular syntax. This article provides a complete overview of the ES6 module system. 1. The current JavaScript module system. JavaScript does not currently have built-in methods to support modularity, but the community has created a very good workaround. Two important (unfortunately mutually incompatible) criteria are: 1. COMMONJS modularity: This
JS File Description 1.1 jqueryDescription: A multi-browser-compatible JavaScript framework that allows you to manipulate document objects, select DOM elements, animate, event handling, use Ajax, and other features.File path: commonjs/jquery-1.9.1.jsCompressed version path: commonjs/jquery-1.9.1.min.jsHow to use: http://jquery.com/var div=$ ("#div")1.2 Jquery-uiDescription: An open source JavaScript Web user
the returned object, these new attributes and methods are available to external callers.The Module mode JavaScript implementation is very concise for people with object-oriented development experience, but it also has its own shortcomings and disadvantages.Because we access public and private members in different ways, when we want to change the visibility, we need to modify the location where this member was used, which is not conducive to maintenance and upgrade, and the coupling is not ideal
journey.Specifications derived from Nodejs Commonjs
2009, Nodejs turned out, creating a new era, people can use JS to write the server's code. If the browser side of the JS even if there is no modularity can be tolerated, the service end is absolutely not.
Daniel gathered in the Commonjs community, developed a modules/1.0 (http://wiki.commonjs.org/wiki/Modules/1.0) specification, for the first time defined
This document is for reading notes.Module specification of COMMONJSNode with the browser and the Organization, CommonJS Organization, ECMAScript the relationship betweenNode Reference CommonJS of the Modules Specification realize a set of module system, so first look at COMMONJS module specification. The COMMONJS defi
Module specification of COMMONJS
The relationship between node and browser, as well as the organization of the COMMONJS, the ECMAScript
Node uses COMMONJS's modules specification to realize a set of modular system, so first look at the COMMONJS module specification.
Commonjs The module definition is very simple, m
This article describes how to deal with the JavaScript Language quot; loop loading quot ;. Currently, the two most common module formats are CommonJS and ES6. The processing method is different, and the returned results are different. "loop loading" (circular dependency) indicates that, the execution of script a depends on script B, and the execution of script B depends on script.
// a.jsvar b = require('b');// b.jsvar a = require('a');
In genera
If you've ever heard of JS modularity, then you should have heard or commonjs or AMD or even cmd, and I've heard it before, but I've heard it before. Now look at what these specs are, and why. A, COMMONJSNBSP;NBSP;COMMONJS is for the performance of JS to develop specifications, because JS does not have the function of the module so CommonJS came into being, it
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.