Where does the "exports" define in PhantomJS?
This question already has an answer here:
The phantomJS implemented require
syntax (same as NodeJS
)
if you want to include external library, that library is injected with module
object and module.exports
is the public object that the require function returns.
//myMoudle.js
var _a = 5; //this is private member of the module
module.exports= {
a : ()=>{
return _a;
},
setA : newA=>_a=newA;
}
The require:
//someCode.js
var myModule = require('path/to/myModule')
myModule.a() //5
myModule._a //undefined
myModule.setA(6) //_a is now 6
PhantomJS docs example requiring webpage module:
var webPage = require('webpage'); //included the module https://github.com/ariya/phantomjs/blob/master/src/modules/webpage.js
var page = webPage.create();
Included the webPage module, inside this module there's the next code
exports.create = function (opts) {
return decorateNewPage(opts, phantom.createWebPage());
};
that allows to use webPage.create
function where we used the require
function