如何从基提供商继承时,美元的化学出版社$ pvent直接访问? [英] How to prevent direct access to properties when inheriting from a base provider?

查看:203
本文介绍了如何从基提供商继承时,美元的化学出版社$ pvent直接访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个跟进<一个href=\"http://stackoverflow.com/questions/30921294/how-to-inherit-from-base-provider-not-the-provider-factory\">How从基地提供者(而不是供应商工厂)继承?。提出的解决方案建议的组合 angular.extend angular.copy (可以只用<$ C完成$ C> angular.merge 对角1.4)到基础提供程序实现复制到所有其他供应商。

This is a follow up to How to inherit from base provider (not the provider factory)?. The proposed solution suggests a combination of angular.extend and angular.copy (which can be done with just angular.merge on Angular 1.4) to copy the base provider implementation into all other providers.

但是,这导致了我没想到另一个问题。利用这种技术,我的供应商现在通过 provider.setX 功能配置,可直接进入 provider.config.x 属性。

But this led to another issue that I was not expecting. With this technique, my providers are now configurable through the provider.setX function, along with direct access to provider.config.x property.

下面是一个例子演示问题:

Here's an example demonstrating the issue:

  • http://codepen.io/anon/pen/ZGXRKr

推荐答案

不能确定你以后,但 this.config.x 不是一个变量但一个属性,这就是为什么它的访问。

Not really sure what you're after, but this.config.x isn't a variable but a property, and that's why it's accessible.

如果您希望它是无法访问,你需要把它声明为一个局部变量无功配置= {} 控制器/服务/工厂/地方的范围内你设置它。因为一个函数/方法中的局部变量都无法访问自己的范围之外,除非他们关闭。

If you want it to be inaccessible, you need to declare it as a local variable var config = {} inside the scope of the controller/service/factory/wherever you're setting it. Because local variables inside a function/method are inaccessible outside their scope, unless they're closures.

这是你的code的一个版本,实例化一个函数 BaseClient (不再供应商虽然,只是一个普通的功能),它包含一个局部变量不能被感动。同样,我不知道这是不是你后,因为我不知道你想解决哪些问题是什么。

Here's a version of your code that instantiates a function BaseClient (no longer a provider though, just a regular function) which contains a local variable that can not be touched. Again, I don't know if this is what you're after since I don't know which problem you're trying to solve.

HTTP://$c$cpen.io/anon/pen/ ZGazqo?编辑= 101

这篇关于如何从基提供商继承时,美元的化学出版社$ pvent直接访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆