嵌套对象内的函数调用失败

时间:2016-09-20 15:34:03

标签: javascript function object scope

在下面的代码中,我想通过构造函数使通用函数可用。该对象用于处理对API的调用。但是如果我从events.POST调用this._isEmpty(obj)它总是返回false。放置_isEmpty()函数的主体确实给出了所需的结果。我该如何解决这个问题? 关于改善这种结构的任何提示都是受欢迎的。 我在SO上查了一些类似的问题,但我发现的问题/答案没有类似的嵌套。

我拨打以下电话:

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'
};

2 个答案:

答案 0 :(得分: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'
};

答案 1 :(得分: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();