简体   繁体   English

如何使用Browserify导出对象?

[英]How to export an object with Browserify?

I started using Browserify and not sure if I completely understand how to use it. 我开始使用Browserify,不确定我是否完全了解如何使用它。

I have a file with some functions bundled in one object in foo.js 我有一个文件,其中某些功能捆绑在foo.js中的一个对象中

var foo = {
  f1: function(){...}
  f2: function(){...}
}

module.exports = function () {
  return foo;
};

And I want to export them to a variable in the main.js file, so I tried doing this: 我想将它们导出到main.js文件中的变量,所以我尝试这样做:

var bar = require('/foo')();

The goal is to be able to do bar.f1() . 目标是能够执行bar.f1() Without executing require('/foo') I get only a function definition, so I have to execute it. 如果不执行require('/foo')我只会得到一个函数定义,因此必须执行它。 Am I doing something wrong? 难道我做错了什么?

Just export the object: 只需导出对象:

var foo = {
  f1: function(){...}
  f2: function(){...}
};

module.exports = foo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM