Search code examples
javascripthapi.js

How javascript call methods working?


Hi I was looking at the core codes of hapijs framework and i noticed this.

 Plugin.call(this, this, [], '', null);

I have no idea why they are passing two this and [], '', null and what it is supposed to do.


Solution

  • Check out this file: https://github.com/hapijs/hapi/blob/master/lib/plugin.js#L21

    As mentioned, the first this is passing context: fun.call(thisArg[, arg1[, arg2[, ...]]])

    Given the function's arguments list (server, connections, env, parent), the final arguments call would be:

    server: this
    connections: []
    env: ''
    parent: null
    

    Also, read up on .call() here: http://mzl.la/18EkRLg