“;出口;在PhantomJS中定义

Where does the "exports" define in PhantomJS?

本文关键字:定义 PhantomJS 出口      更新时间:2023-09-26

以fs.js的代码段为例:

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();
};

现在我和exports对象混淆了。你可以在每个PhantomJS模块中找到它,但我找不到在哪里定义exports对象。

有人能给我一些关于定义exports对象的地方的建议吗?


不要和NodeJS中的exports混淆。这是PhantomJS。。。

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文档示例需要网页模块:

var webPage = require('webpage'); //included the module https://github.com/ariya/phantomjs/blob/master/src/modules/webpage.js 
var page = webPage.create();

包括网页模块,在这个模块中有下一个代码

exports.create = function (opts) {
    return decorateNewPage(opts, phantom.createWebPage());
};

允许使用webPage.create函数,其中我们使用了require函数