Javascript私有函数和object.prototype扩展

时间:2014-10-23 09:24:59

标签: javascript angularjs prototype extend

我有以下angularJS服务

define(["angular"], function(Angular) {

var dataStorageService = function() {
    var serviceConstructor = function() {
        var _getColor = function(color) {
            return this.config.categoryColorMapping.colors[color];
        }
    }

    var serviceInstance = new serviceConstructor();

    angular.extend(serviceInstance.prototype, {
        config: {
            numberOfMessagesDisplayed: 5,
            maxTitleLength: 48,
            maxPreambleLength: 140,
            categoryColorMapping: {
                colors : {
                    nyheter: '#2B2B2B',
                    sport: '#F59331',
                    underholding: '#F9B00D'
                },
                categories: {
                    nyheter: _getColor('nyheter'),
                    sport: _getColor('sport'),
                    underholding: _getColor('underholding')
                }
            }
        },
        get: function(param) {
            if(this.config.hasOwnProperty(param)) {
                return this.config[param];
            } else {
                console.warn('Playlist::configService:no "' + param + '" config found');
                return false;
            }
        },
        set: function(param, value) {
            this.config[param] = value;
        }
    });

    return serviceInstance;
};

return dataStorageService;
});

现在我的目标是公开以下方法:

  1. 获得
  2. 设置
  3. 我希望'_getColor'方法私有,但我想在JSON对象配置中使用它。当我运行代码时,我有

    “ReferenceError:_getColor未定义”

    是否有可能以这种方式实现它? (要使_getColor为private并在angular.extend中的JSON对象中使用它?)

3 个答案:

答案 0 :(得分:1)

添加到prototype的函数是在构造函数的词法范围之外定义的,因此无法访问“私有”方法。

前者在所有实例之间共享,后者是每个实例。解决这个问题的唯一方法是显式地将(每个实例)函数导出为实例的属性,使其成为非私有函数。

答案 1 :(得分:1)

函数可以共享,但仍然是私有的,但是必须在构造函数中定义特定于实例的私有成员。由于您的私有函数不需要访问特定于实例的私有成员,因此您可以执行以下操作:

define(["angular"], function(Angular) {

var dataStorageService = function() {
    var serviceConstructor = function() {
    }

    var serviceInstance = new serviceConstructor();
    //IIFE returning object that will have private members as closure
    // privileged methods have to be in the same function body as the
    // private fucnction
    serviceInstance.prototype = (function() {
        var _getColor = function(instance, color) {
            return instance.config.categoryColorMapping.colors[color];
        };
        return {
          constructor: serviceConstructor
          ,config: {
            numberOfMessagesDisplayed: 5,
            maxTitleLength: 48,
            maxPreambleLength: 140,
            categoryColorMapping: {
                colors : {
                    nyheter: '#2B2B2B',
                    sport: '#F59331',
                    underholding: '#F9B00D'
                },
                categories: {
                    //since categories is a sub object of serviceinstance.categorycolormapper
                    // it is not possible to get the instance of serviceinstance
                    // at this time unless you set it in the constructor
                    // solution could be that each serviceinstance has it's own categorycolormaper
                    // and when categorycolormapper is created pass the serviceinstance instance
                    nyheter: _getColor(this,'nyheter'),
                    sport: _getColor(this, 'sport'),
                    underholding: _getColor(this, 'underholding')
                }
            }
          },
          get: function(param) {
            if(this.config.hasOwnProperty(param)) {
                return this.config[param];
            } else {
                console.warn('Playlist::configService:no "' + param + '" config found');
                return false;
            }
          },
          set: function(param, value) {
            this.config[param] = value;
          }
        }
    }());

    return serviceInstance;
};

return dataStorageService;
});

有关构造函数和原型的更多信息,请访问:https://stackoverflow.com/a/16063711/1641941

答案 2 :(得分:0)

serviceConstructor的定义中,在定义_getColor

之后添加以下行
serviceConstructor.prototype._getColor = _getColor ;