javascript - Making use of browserified NodeJS file into browser -


i have nodejs file browserfied using browserify module. used following command

browserify testfile.js -o bundle.js 

to make use of file in browser, making use of window object.

assume below code generated after browserifying file.

var main = function () {    this.foo = function () {    },    this.temp= function () {    },    this.bar= function () {    } } 

to make use changed

window.main = function () {        this.foo = function () {        },        this.temp= function () {        },        this.bar= function () {        }     } 

and use of these functions, used following code:

var obj= new main (); 

and can obj.foo(); or obj.bar();

all working fine wonder if right way call function browserfied file.

please suggest correct way make use of browserfied file.

browserify great tool when use entire project. makes no sense use single files only. whole point of avoiding globals, instead of setting window.main this:

module.exports = function () {    this.foo = function () {    },    this.temp= function () {    },    this.bar= function () {    } } 

and in files need access above, do:

var main = require('./path/to/main.js'); 

browserify resolves , inlines require calls automatically need run browserify on single file fires app.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -