passing a default argument to a browserify module

☆樱花仙子☆ 提交于 2019-12-11 02:53:55

问题


I'm refactoring a javascript codebase and am implementing, but I'm new to node. I might run into some code like this:

foo.js

var foo = {};
foo.bar = function(baz) {
    $('body').append(baz)
}

which i would then refactor into the following:

foo.js

var $ = require('jquery')(window);
var foo = {};
foo.bar = require('./bar');

bar.js

module.exports = bar = function(baz) {
    $('body').append(baz);
}

What's the correct way to pass the jQuery object from foo.js to bar.js without interfering with the baz parameter when foo.bar(baz) is called?


回答1:


Just add var $ = require('jquery')(window) to each module that needs jQuery!

Calls to require that resolve to the same path will return a cached copy of the module:

http://nodejs.org/api/modules.html#modules_caching



来源:https://stackoverflow.com/questions/23376969/passing-a-default-argument-to-a-browserify-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!