2017-08-03 260 views
-2

我有很不錯的js的背景,但我是新來的Node.js 我不undersand爲什麼簡單的類對象的功能並沒有叫node.js的內部函數類對象調用函數

function functions() { 

     function test() { 
      console.log("function ok"); 

      function test2() { 
       console.log("function inside function is ok"); 
      } 
      return { 
       test2 : test2 
      }; 
     } 

     return { 
      test : test 
     }; 

    } 

var test_function = new functions(); 
functions.test.test2(); 

我得到錯誤

TypeError: Cannot read property 'test2' of undefined 

感謝

回答

1

嘗試調用test_function.test().test2()。您需要調用test(),然後才能調用test2()。此外,在您的示例中,您正確地調用了functions()並將其分配給了test_function,但是您沒有對它做任何處理。