2016-09-20 73 views
0

在下面的代碼中,我想通過構造函數使泛型函數可用。該對象用於處理對API的調用。但是如果我從events.POST調用this._isEmpty(obj),它總是返回false。放置_isEmpty()函數的主體確實給出了期望的結果。我該如何解決這個問題? 改善這種結構的任何提示都是可以接受的。 我查找了一些類似的問題,但我發現的問題/答案沒有類似的嵌套。從嵌套對象中失敗的函數調用

我提出以下電話:

var assystBridge = new global.assystBridge(); 
response.setStatus(assystBridge.events.POST(request, response)); 

對象:

var assystBridge = Class.create(); 
assystBridge.prototype = { 
    initialize: function() { 
     // Generic functions go here 
     function _isEmpty(obj){ 
      // Checks whether an object has properties 
      // Used for checking whether any data was posted or not 
      // Returns true or false 
      return Object.keys(obj).length === 0; 
     } 
    }, 
    events: { 
     POST: function(request, response){ 
      var data = request.body.data; 
      var responseText = ''; 
      if(this._isEmpty(data)){ 
      //if(Object.keys(data).length === 0){ 
       response.setBody({"response":"NO data was posted", "data": data}); 
       return 400; 
      } else { 
       return 201; 
      } 

     }, 
     GET: function(request, response){ 

     }, 
     DELETE: function(request, response){ 

     } 
    }, 
    type: 'assystBridge' 
}; 
+0

你如何調用events.POST?你是否實例化了assystBridge類,然後調用instance.events.POST? 我在問你爲什麼要在事件中確定「this」的含義。 – klikas

+0

更新了問題 –

+0

我很驚訝,當您撥打 response.setStatus(assystBridge.events.POST(request,response))時,您不會收到錯誤; 如果以某種方式assystBridge不從其他地方繼承_isEmpty函數,代碼應該正常中斷,因爲它的寫法_isEmpty不是類或成員的原型,它只是initialize方法的私有成員。 – klikas

回答

1

如果移動通用函數了initialize()方法,並將它是原型的屬性之一,然後你可以通過assystBridge.prototype._isEmpty()來引用它。

例如:

assystBridge.prototype = { 
    _isEmpty: function(obj) { 
     // Checks whether an object has properties 
     // Used for checking whether any data was posted or not 
     // Returns true or false 
     return Object.keys(obj).length === 0; 
    }, 
    events: { 
     POST: function(request, response){ 
      var data = request.body.data; 
      var responseText = ''; 
      if(assystBridge.prototype._isEmpty(data)){ 
       response.setBody({"response":"NO data was posted", "data": data}); 
       return 400; 
      } else { 
       return 201; 
      } 

     }, 
    }, 
    type: 'assystBridge' 
}; 
+0

所以我做了一個頂級屬性genericFunctions,它將容納所有泛型函數,並將var gf = assystBridge.prototype.genericFunctions添加到events.POST。現在gf._isEmpty()起作用。謝謝! –

1

再說什麼已經已經說了,同時被錄取了太多,你甚至可以動一步,創造了assystBridge工廠,因而是在處理函數範圍內更好地控制適當...

var createAssystBridge = (function() { // - immediately invoked function expression 
             // acting as a generator for a factory that 
             // will be returned, thus creating a closure ... 

    // ... due to shared code, that will be preserved within this closure. 

    function isEmpty(obj) { 
     // Checks whether an object has properties 
     // Used for checking whether any data was posted or not 
     // Returns true or false 
     return Object.keys(obj).length === 0; 
    } 

    function postData(request, response) { 
     var data = request.body.data; 
     var responseText = ''; 
     if (isEmpty(data)){ 
      response.setBody({"response":"NO data was posted", "data": data}); 
      return 400; 
     } else { 
      return 201; 
     } 
    } 
    function getData(request, response) { 

    } 
    function deleteData(request, response) { 

    } 

    function initializeAssystBridge() { 

    } 

    // returning the factory 

    return function assystBridgeFactory() { 
     var 
      assystBridge = Class.create(); 
      //assystBridge = Object.create({}); 

     assystBridge.prototype = {    // - assign shared code 
      initialize: initializeAssystBridge, // with every call 
      events: {       // of this factory. 
       POST : postData,    // 
       GET  : getData,    // 
       DELETE : deleteData   // 
      }, 
      type: 'assystBridge' 
     }; 
     return assystBridge; 
    }; 

}()); 

var assystBridge = createAssystBridge(); 
+0

非常有趣。我會放棄這一點。 TNX! –