使用 node.js 导出类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18020113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exporting classes with node.js
提问by mattr-
I have the following test code that is being ran by jasmine-node in a file called bob_test.spec.js
我有以下测试代码由 jasmine-node 在一个名为的文件中运行 bob_test.spec.js
require('./bob');
describe("Bob", function() {
var bob = new Bob();
it("stating something", function() {
var result = bob.hey('Tom-ay-to, tom-aaaah-to.');
expect(result).toEqual('Whatever');
});
});
In order to make the test pass, I've written the following production code in a file called bob.js
为了使测试通过,我在名为的文件中编写了以下生产代码 bob.js
"use strict";
var Bob = function() {
}
Bob.prototype.hey = function (text) {
return "Whatever";
}
module.exports = Bob;
When I run the test - using jasmine-node .- I get the following
F
当我运行测试时 - 使用jasmine-node .- 我得到以下 F
Failures:
1) Bob encountered a declaration exception
Message:
ReferenceError: Bob is not defined
Stacktrace:
ReferenceError: Bob is not defined
at null.<anonymous> (/Users/matt/Code/oss/deliberate-practice/exercism/javascript/bob/bob_test.spec.js:4:17)
at Object.<anonymous> (/Users/matt/Code/oss/deliberate-practice/exercism/javascript/bob/bob_test.spec.js:3:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
Finished in 0.02 seconds
1 test, 1 assertion, 1 failure, 0 skipped
Based on what I understand about Javascript, I feel like this should work. What does node.js do differently with constructor functions and module exports that prevents this from working I like think it should?
根据我对 Javascript 的了解,我觉得这应该可行。node.js 对构造函数和模块导出做了什么不同的事情,阻止它工作,我认为应该这样做?
回答by mrvn
require returns an object, you should store it somewhere
require 返回一个对象,你应该把它存储在某个地方
var Bob = require('./bob');
and then use this object
然后使用这个对象
var bobInstance = new Bob();
回答by Jonas Brandel
If you can use ECMAScript 2015 you can declare and export your classes and then 'import' your classes using destructuring with no need to use an object to get to the constructors.
如果您可以使用 ECMAScript 2015,您可以声明和导出您的类,然后使用解构“导入”您的类,而无需使用对象来访问构造函数。
In the module you export like this
在你这样导出的模块中
class Person
{
constructor()
{
this.type = "Person";
}
}
class Animal{
constructor()
{
this.type = "Animal";
}
}
module.exports = {
Person,
Animal
};
then where you use them
那么你在哪里使用它们
const { Animal, Person } = require("classes");
const animal = new Animal();
const person = new Person();
回答by user2647543
This should fix the error you were having while running your tests via jasmine-node:
这应该可以解决您在通过 jasmine-node 运行测试时遇到的错误:
// Generated by CoffeeScript 1.6.2
(function() {
var Bob;
Bob = (function() {
function Bob() {}
Bob.prototype.hey = function(what) {
return 'Whatever.';
};
return Bob;
})();
module.exports = Bob;
}).call(this);
回答by Novitoll
Improving marvin'sanswer:
改进马文的答案:
"use strict";
var Bob = function() {}
Bob.prototype.hey = function (text) {
return "Whatever";
}
module.exports = new Bob();
// another file
var Bob = require('./bob');
Bob.hey('text');
So you can create an object passing it to module.exports module.exports = new Bob();
所以你可以创建一个对象将它传递给 module.exports module.exports = new Bob();

