Take a code segment of the fs.js for example:
exports.write = function (path, content, modeOrOpts) {
var opts = modeOrOptsToOpts(modeOrOpts);
// ensure we open for writing
if ( typeof opts.mode !== 'string' ) {
opts.mode = 'w';
} else if ( opts.mode.indexOf('w') == -1 ) {
opts.mode += 'w';
}
var f = exports.open(path, opts);
f.write(content);
f.close();
};
Now I'm confused with the exports
object. You can find it in every PhantomJS module but I found no where to define the exports
object.
Could anyone give me some suggestions about the place where defined the exports
object?
Don't be confused with the exports
in NodeJS. It's PhantomJS...
答案 0 :(得分:1)
phantomJS实现了require
语法(与NodeJS
相同)
如果要包含外部库,则该库注入module
对象,module.exports
是require函数返回的公共对象。
//myMoudle.js
var _a = 5; //this is private member of the module
module.exports= {
a : ()=>{
return _a;
},
setA : newA=>_a=newA;
}
要求:
//someCode.js
var myModule = require('path/to/myModule')
myModule.a() //5
myModule._a //undefined
myModule.setA(6) //_a is now 6
PhantomJS docs示例 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();
包含webPage模块,在此模块中有下一个代码
exports.create = function (opts) {
return decorateNewPage(opts, phantom.createWebPage());
};
允许在我们使用webPage.create
函数
require
函数