Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 服务中的只读属性_Javascript_Angularjs - Fatal编程技术网

Javascript 服务中的只读属性

Javascript 服务中的只读属性,javascript,angularjs,Javascript,Angularjs,我有一个缓存服务,我想在其中存储数据 angular.module('core').service('markerCache', function(){ var cache = []; this.set_cache = function(arr){ cache = arr; this.cached = true; }; this.cached = false; //this must be read-only and must be

我有一个缓存服务,我想在其中存储数据

angular.module('core').service('markerCache', function(){
    var cache = [];
    this.set_cache = function(arr){
        cache = arr;
        this.cached = true;
    };
    this.cached = false; //this must be read-only and must be property, not function
});
如果可能的话,我还想在angular services(工厂、服务)中定义只读(值只能在服务内部设置)属性,如果没有,任何解决方法都会非常棒。
在javascript中定义只读属性的一种方法,但如何以角度的方式进行?
只读是指在服务内部设置值。比如说

angular.module('core').controller('Ctrl', function($scope, markerCache){
    markerCache.cached = false; //disable setter outside of markerCache service
});
更新,对于那些仍然感兴趣的人,这里是工作服务 你在找什么

常量(名称、值);注册一个常量服务,例如字符串, 带有$injector的数字、数组、对象或函数。 与值不同,它可以注入到模块配置函数中 (请参见angular.Module)并且它不能被angular 装饰师

你在找什么

常量(名称、值);注册一个常量服务,例如字符串, 带有$injector的数字、数组、对象或函数。 与值不同,它可以注入到模块配置函数中 (请参见angular.Module)并且它不能被angular 装饰师


您可以使用闭包隐藏变量,并使用公开属性来读取变量

angular.module('core').service('markerCache', function(){
    var cache = [];
    var cached = false; // private variable
    this.set_cache = function(arr){
        cache = arr;
        cached = true;
    };
    Object.defineProperty(this, "cached", { // public readonly getter
      get: function() {
        return cached;
      },
      set: function(val) {
        //throw new Error('Cannot set internal cache state'); //throw custom exception
      }
      //set: angular.noop //or do nothing, empty setter
    })
});

您可以使用闭包隐藏变量,并使用公开属性来读取变量

angular.module('core').service('markerCache', function(){
    var cache = [];
    var cached = false; // private variable
    this.set_cache = function(arr){
        cache = arr;
        cached = true;
    };
    Object.defineProperty(this, "cached", { // public readonly getter
      get: function() {
        return cached;
      },
      set: function(val) {
        //throw new Error('Cannot set internal cache state'); //throw custom exception
      }
      //set: angular.noop //or do nothing, empty setter
    })
});

变量不应与
上下文关联,您可以通过将其设置为
var cached
,并在服务内部定义
getter
,以获取
cache
的值,从而使消费者可以通过
getter
函数访问该值

angular.module('core').service('markerCache', function(){
    var cache = [], 
        cached = false; //made privet
    this.set_cache = function(arr){
        cache = arr;
        cached = true;
    };
    this.getCache = function(){
        return cached;
    }
});

变量不应与
上下文关联,您可以通过将其设置为
var cached
,并在服务内部定义
getter
,以获取
cache
的值,从而使消费者可以通过
getter
函数访问该值

angular.module('core').service('markerCache', function(){
    var cache = [], 
        cached = false; //made privet
    this.set_cache = function(arr){
        cache = arr;
        cached = true;
    };
    this.getCache = function(){
        return cached;
    }
});

很好的解决方案,任何摆脱
的解决方案都无法设置[object object]的属性cached,它只有一个getter
,我的意思是这是正确和准确的?但是如何显示自定义消息呢?最优雅的解决方案好的解决方案,任何摆脱
的解决方案都不能设置只有getter的[object object]的属性cached,我的意思是这是正确和准确的?但如何显示自定义消息?最优雅的解决方案