Browserify - How to call function bundled in a file generated through browserify in browser

前端 未结 11 2219
-上瘾入骨i
-上瘾入骨i 2020-11-29 17:18

I am new to nodejs and browserify. I started with this link .

I have file main.js which contains this code

var unique = require(\'uniq\');

var data          


        
11条回答
  •  被撕碎了的回忆
    2020-11-29 17:44

    I just read through the answers and seems like nobody mentioned the use of the global variable scope? Which is usefull if you want to use the same code in node.js and in the browser.

    class Test
    {
      constructor()
      {
      }
    }
    global.TestClass = Test;
    

    Then you can access the TestClass anywhere.

    
    
    

    Note: The TestClass then becomes available everywhere. Which is the same as using the window variable.

    Additionally you can create a decorator that exposes a class to the global scope. Which is really nice but makes it hard to track where a variable is defined.

提交回复
热议问题