将默认参数传递给browserify模块 [英] passing a default argument to a browserify module

查看:37
本文介绍了将默认参数传递给browserify模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构一个javascript代码库并正在实现,但我是节点的新手。我可能遇到这样的代码:

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)
}

我将重构为以下内容:

foo.js

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

bar.js

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

将jQuery对象从foo.js传递到bar的正确方法是什么。当foo.bar(baz)被调用时,js没有干扰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?

推荐答案

只需添加 var $ = require('jquery')(window)到每个需要jQuery的模块!

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

调用 require解析为相同路径的将返回模块的缓存副本:

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

这篇关于将默认参数传递给browserify模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆