Where does the "exports" define in PhantomJS?

This question already has an answer here:

  • Is 'require(…)' a common javascript pattern or a library function? 3 answers
  • What is the purpose of Node.js module.exports and how do you use it? 10 answers

  • 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

    链接地址: http://www.djcxy.com/p/96618.html

    上一篇: node.js导出调用控制器内的另一个控制器方法

    下一篇: “出口”在PhantomJS中定义在哪里?