Use PhoneGap fast two years, only recently impulse to see the source code, is too ashamed, can only blame themselves or a JS rookie. So prepare to write something and record it while learning.
I now use the Cordova3.5 version (now 3.6), JS code is probably divided into two pieces: Cordova.js corresponds to the basic framework of Cordova, Android native code is a separate project, as the library is referenced, you can also export jar use; Cordova The plugin module under _plugin.js and plugins, which corresponds to the Java code under the project Org.apache.cordova, can be used to interact with the Android native code and, of course, to define the plugin itself.
First look at Cordova.js, the beginning defines two methods:
varrequire, define; (function() { //map objects for each module varModules = {}, //Module Request StackRequirestack = [], //The map object in the Requirestack indexInprogressmodules ={}, SEPARATOR= "."; functionBuild (module) {varFactory =module.factory, Localrequire=function(ID) {varResultantid =ID; //Module ID uses a relative path if(Id.charat (0) = = = ".") {Resultantid= Module.id.slice (0, Module.id.lastIndexOf (SEPARATOR)) + SEPARATOR + id.slice (2); } returnrequire (Resultantid); }; Module.exports= {}; Deletemodule.factory; Factory (Localrequire, module.exports, module); returnModule.exports; } Require=function(ID) {if(!Modules[id]) { Throw"MODULE" + ID + "not found"; } Else if(IDinchinprogressmodules) { //when using the module, there is the case of cyclic require, this time should check your module division. varCycle = Requirestack.slice (Inprogressmodules[id]). Join (', ') + ' + 'ID; Throw"Cycle in require graph:" +cycle; } if(modules[id].factory) {Try{Inprogressmodules[id]=requirestack.length; Requirestack.push (ID); returnbuild (Modules[id]); } finally { DeleteInprogressmodules[id]; Requirestack.pop (); } } //when the module is requested, it is no longer necessary to execute the factory method. returnModules[id].exports; }; Define=function(ID, factory) {if(Modules[id]) {Throw"MODULE" + ID + "already defined"; } Modules[id]={id:id, factory:factory}; }; Define.remove=function(ID) {DeleteModules[id]; }; Define.modulemap=modules; })();
Right is require and define, front-end small partners must be very familiar with, and certainly more than I know. But Cordova the two methods here do not implement modular loading, you have to put JS all in.
Define is the definition module,
function (Require, exports, module) {});
Require is the use of modules,
var cordova = require (' Cordova ');
Look at the code to see the implementation of define, and then see the implementation of require, and finally look at the build method.
Cordova the first part of this, it is not difficult.
Novice Learning Cordova-android JS Source (1)