0

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...

Sayakiss
  • 6,878
  • 8
  • 61
  • 107
  • http://stackoverflow.com/questions/9901082/what-is-this-javascript-require it's an object that injected to the module by requiring it – Daniel Krom May 17 '16 at 11:21

1 Answers1

2

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

Daniel Krom
  • 9,751
  • 3
  • 43
  • 44
  • "The phantomJS implemented require syntax", could you please show me the code in PhantomJS which implements the `require` syntax? – Sayakiss May 17 '16 at 11:41
  • @Sayakiss added in my answer – Daniel Krom May 17 '16 at 11:47
  • I'm still confused... In my opinion, `require` is a function, so it should be defined somewhere(but I can't find)... `exports` is a object, so it should be defined somewhere(but I can't find, either)... That's what really bother me... – Sayakiss May 17 '16 at 11:52
  • @Sayakiss module is an object that is injected to your module by the compiler (there's no compiler for JS and it's interpreter but nvm), just like `window` in browser, `module.exports` is the object that is returned from the require operation – Daniel Krom May 17 '16 at 11:56
  • @Sayakiss think about it as a `class` instead of `public` your write `exports`. `public func foo()` becomes `exports.foo = function`, the rest is private (but it's all static, if you require same module many times, it is created only once) – Daniel Krom May 17 '16 at 11:57
  • Okay, now I see that... But PhantomJS doesn't use NodeJS... I think PhantomJS implemented a similar one... So I think it's not `nvm` as you mentioned... – Sayakiss May 17 '16 at 12:00
  • @Sayakiss of course it doesn't use `NodeJS` I meant the require syntax is same as `NodeJS`. NodeJS is a program just like phantomJS. `NodeJS` uses V8 JS engine, and `PhantomJS` uses QtWebKit – Daniel Krom May 17 '16 at 12:04