Tag:

模块导出类Nodes.js

我是一个Nodes.js noob,我试图让我的头模块结构。 到目前为止,我有一个模块(testMod.js)定义了这个类的构造: var testModule = { input : "", testFunc : function() { return "You said: " + input; } } exports.test = testModule; 我试图调用testFunc()方法: var test = require("testMod"); test.input = "Hello World"; console.log(test.testFunc); 但是我得到一个TypeError: TypeError: Object #<Object> has no method 'test' 我做错了什么?

在nodejs项目中使用es6类的正确方法

我希望能够使用nodejs 4.1.2的酷es6类function 我创build了以下项目: a.js: class a { constructor(test) { a.test=test; } } index.js: require('./a.js'); var b = new a(5); 你可以看到我创build一个简单的类,它的构造函数获取一个参数。 并在我包括我需要这个类,并创build一个新的对象基于该类。 很简单..但仍然我得到以下错误: SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:413:25) at Object.Module._extensions..js (module.js:452:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require […]