TypeError: Object is not a function at Object.<anonymous> 构造 Javascript w/new 时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20992480/
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
TypeError: Object is not a function at Object.<anonymous> when constructing Javascript w/ new
提问by Adron
I've put together a little code in one file, which is below:
我将一些代码放在一个文件中,如下所示:
var exports = Symphonize;
function Symphonize(generation_specification) {
this.generate_spec = function(){
return generation_specification;
}
}
When I start another JavaScript file in the same project and require the code like so:
当我在同一个项目中启动另一个 JavaScript 文件并需要这样的代码时:
var symphonize = require('../bin/symphonize');
var Symp = new symphonize({"test":"test1"});
It throws the error:
它抛出错误:
/Users/adron/Codez/symphonize/tests/symphonize.js:8
var Symp = new symphonize({"test":"test1"});
^ TypeError: object is not a function at Object.<anonymous>
/Users/adron/Codez/symphonize/tests/symphonize.js:8
var Symp = new symphonize({"test":"test1"});
^ TypeError: object is not a function at Object.<anonymous>
Is there another way I should be constructing this? I just want the "symphonize" code to require a simple object (an object of configuration that will be JSON) before any functions on that code are called.
我应该用另一种方式构建它吗?我只希望“symphonize”代码在调用该代码上的任何函数之前需要一个简单的对象(一个将是 JSON 的配置对象)。
回答by pllee
When setting the exports something you must do module.exports = Something. You should do something like :
设置导出时,您必须执行 module.exports = 某事。你应该做这样的事情:
module.exports = Symphonize;
If you had Symphonize as a property on the exports module.exports is not needed
如果您将 Symphonize 作为导出模块的属性,则不需要导出
exports.Symphonize = Symphonize;
Use it in a file.
在文件中使用它。
var Symphonize = require('../bin/symphonize').Symphonize
Also var exports
is kind of ambiguous statement in Node.
在 Node.js 中var exports
也是一种模棱两可的声明。