execute function that is inside a JavaScript extract

2

I'm trying to use functions that are inside an export, for example BigInteger , in fact all are:

link

But I need to use them in another file so I do not have to repeat it, is it possible?

    
asked by anonymous 12.04.2015 / 00:15

1 answer

4

It will not be possible to access all internal functions, but you can access what is exported from this module.

Explanation: When you have a structure of type:

(function (exports) {
    function a() {
        alert('oi');
    }
})(escopo);

This a function is within a scope that does not allow it to be accessible outwards. This is used a lot to limit the scope of functions and does not pollute global space with variable names.

Notice that in this file you mentioned it ends with:

exports.JSEncrypt = JSEncrypt;
})(JSEncryptExports);

that is, it exports to the object / variable that is passed to JSEncryptExports .

So some functions will never have access, but the methods that are exposed you can find in JSEncryptExports . And that way you can reuse the code.

It's common to use this when exporting to global space:

(function(global){
     // código A aqui...
     global.meuModulo = function(a, b, c){
         // código que tem acesso aos métodos defenidos no "código A"
     }
})(typeof exports != 'undefined' ? exports : window);

This way works both in NodeJS and Browser.

    
12.04.2015 / 00:29