Home  >  Article  >  Web Front-end  >  Analysis of module dependency loading processing examples in seajs

Analysis of module dependency loading processing examples in seajs

小云云
小云云Original
2018-01-26 10:47:391223browse

This article mainly introduces the loading processing of module dependencies in seajs, and analyzes the principles, related precautions and usage of seajs module dependencies and loading in the form of examples. Friends in need can refer to it. I hope it can help everyone.

Recently, when I was working on a project, I discovered some module dependency issues, which I recorded below:

For example, there are three files:

/*init.js*/
define(function(require, exports, module){
 require('jquery');
 require('jquery.plugA');
})
/*jquery.plugA.js*/
define(function(require, exports, module){
 require('jquery');
 require('jquery.plugB');
 //code...
})
/*jquery.plugB.js*/
define(functioin(require, exports, module){
 require('jquery');
 //code...
})

For example, when executing init.js, init.js, jquery.plugA.js, jquery.plugB.js all depend on jquery, so how does seajs handle jquery in this case? Executed only once? Execute multiple times? Or some other way?

Refer to Yubo's answer here:

My understanding of module calling is that calling refers to obtaining the interface of a certain module. In SeaJS, only seajs.use, require.async, and require will generate module calls, for example: var a = require('./a') When executing require('./a'), the module's interface will be obtained , if it is called for the first time, module a will be initialized, and when called later, the interface of module a will be returned directly; define just registers module information, for example, after packaging: define(id, deps, factory) registers a module to seajs In .cache, define is similar: seajs.cache[id] = { id: id, dependencies: deps, factory: factory }

is pure registration information.

Seajs.cache['a'].factory will be executed only when require('./a') is executed, and seajs.cache['a'].exports

will be obtained after execution. Related recommendations:

Detailed explanation of the parsing rules of modules in seajs and summary of module usage_Seajs

The above is the detailed content of Analysis of module dependency loading processing examples in seajs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn