2012-01-26 2 views
9

저는 Nodes.js 멍청한 놈입니다. 모듈 구조에 대해 머리를 쓰려고합니다. 내가 thusly 히 testFunc() 메서드를 호출하려고모듈 내보내기 클래스 Nodes.js

var testModule = { 
    input : "", 
    testFunc : function() { 
     return "You said: " + input; 
    } 
} 

exports.test = testModule; 

: 지금까지 내가 모듈 (testMod.js)이이 클래스의 구조를 정의해야

var test = require("testMod"); 
test.input = "Hello World"; 
console.log(test.testFunc); 

그러나 나는 형식 오류 얻을 :

TypeError: Object #<Object> has no method 'test' 

나는 무엇을 잘못하고 있니?

답변

11

네임 스페이스 문제입니다. 지금 : 대신 exports.test의 당신은, module.exports = testModule을 할 수있는,

var test = require("testMod"); // returns module.exports 
test.test.input = "Hello World"; // sets module.exports.test.input 
console.log(test.test.testFunc); // returns function(){ return etc... } 

을 또는 :

var test = require("testMod"); // returns module.exports 
test.input = "Hello World"; // sets module.exports.input 
console.log(test.testFunc); // error, there is no module.exports.testFunc 

당신이 할 수있는 최고

var test = require("testMod"); // returns module.exports (which is the Object testModule) 
test.input = "Hello World"; // sets module.exports.input 
console.log(test.testFunc); // returns function(){ return etc... } 
+0

감사합니다. – travega