- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
根据我对 javascript 的理解,原型(prototype)方法不能访问构造函数范围内私有(private)的变量,
var Foo = function() {
var myprivate = 'I am private';
this.mypublic = 'I am public';
}
Foo.prototype = {
alertPublic: function() { alert(this.mypublic); } // will work
alertPrivate: function() { alert(myprivate); } // won't work
}
这很有道理,但有没有什么安全且好的方法可以解决这个问题?由于使用原型(prototype)提供了性能优势,因为成员函数只分配一次,所以我想实现类似的功能,同时仍然能够访问我的私有(private)变量。我不认为它会通过使用原型(prototype)来工作,但是是否有另一种模式,例如工厂方法或闭包方法?类似的东西,
var fooFactory = function() {
var _alertPrivate = function(p) { alert(p); } // bulk of the logic goes here
return function(args) {
var foo = {};
var myprivate = args.someVar;
foo.mypublic = args.someOtherVar;
foo.alertPrivate = function() { _alertPrivate(myprivate); };
return foo;
};
}
var makeFoo = new fooFactory();
var foo = makeFoo(args);
我不确定每次创建新 Foo 时是否创建了 _alertPrivate 的新副本,或者是否有任何潜在的性能优势。目的是获得类似于原型(prototype)的功能(因为它可以节省内存),同时仍然能够访问私有(private)变量。
谢谢。
最佳答案
我想出了以下模式来解决这个问题,至少现在是这样。我需要的是一个特权 setter ,以便可以从某些原型(prototype)函数内部更改私有(private)变量,但不能从其他任何地方更改:
var Foo = (function() {
// the bulk of the objects behavior goes here and is created once
var functions = {
update: function(a) {
a['privateVar'] = "Private variable set from the prototype";
}
};
// the objects prototype, also created once
var proto = {
Update: function() {
this.caller('update');
}
};
// special function to get private vars into scope
var hoist = function(accessor) {
return function(key) {
return functions[key](accessor());
}
}
// the constructor itself
var foo = function foo() {
var state = {
privateVar: "Private variable set in constructor",
// put more private vars here
}
this.caller = hoist(function(){
return state;
});
}
// assign the prototype
foo.prototype = proto;
// return the constructor
return foo;
})();
基本上,一个指向对象内部状态的指针通过一个简单访问器上的闭包被提升到它的原型(prototype) function() { return state; }.在任何给定实例上使用“调用者”函数允许您调用仅创建一次但仍可以引用该实例中保存的私有(private)状态的函数。同样重要的是要注意,原型(prototype)之外的任何函数都不能访问特权访问器,因为“调用者”只接受一个键,该键引用回范围内的预定义函数。
以下是此方法的一些基准,以了解它与纯原型(prototype)制作的比较。这些数字表示在一个循环中创建对象的 80,000 个实例(注意用于基准测试的对象比上面的对象更复杂,这只是为了简化目的):
Chrome :
仅关闭 - 2172 毫秒
原型(prototype)制作(上述方式)- 822 毫秒
原型(prototype)制作(标准方式)- 751 毫秒
火狐:
仅关闭 - 1528 毫秒
原型(prototype)制作(上述方式)- 971 毫秒
原型(prototype)制作(标准方式)- 752ms
如您所见,该方法几乎与普通原型(prototype)制作一样快,而且绝对比仅使用将函数与实例一起复制的普通闭包更快。
关于javascript - 可以从函数原型(prototype)访问私有(private)构造函数范围的变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7788175/
如果需要在类外访问静态(例如单例),可以选择公共(public)静态而不是私有(private)静态,而当不需要公开函数时首选私有(private)静态(否则未命名的命名空间就可以了)——在这种情况下
在互联网上进行了一些搜索,但找不到简单的答案。我的问题集是在 Android 框架中使用 Java,但我相信这也是标准的 Java 行为。我理解 final 和 private 的定义,它们都用于变量
我有这个代码: public final class Board { private final int[][] blocks; private final int N; pr
对我来说,过去作为 Objective-C 开发人员很简单。一个类需要公开的每个字段都是一个属性,每个私有(private)字段都是一个没有 getter 或 setter 的实例变量。但我经常看到人
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一个在 Docker 容器中运行的应用程序。它需要来自公司私有(private) NPM 注册表(Sinopia)的一些私有(private)模块,并且访问这些需要用户身份验证。 Dockerfi
我试图理解 C# 使用 getters 和 setters 自动声明变量与 java 声明之间的区别。 在java中我通常这样做: private int test; public int getTe
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我目前正在使用 Objective-C(适用于 iPhone)构建游戏。 为此,出于性能/复杂性原因,我略微打破了 MVC,并为 View (渲染器)提供了对模型的直接引用。这是因为它应该以 60fp
我已经在 ubuntu 上成功配置了 2 个虚拟主机站点(基于名称的虚拟主机)。我的 apache 版本是 2.2.22。 这两个站点都在本地主机上工作。 /etc/hosts 条目 127.0.0.
考虑下面的类 public class A { private final Map cache; public HeavyObject getThing(); } 假设不能泄漏对缓存
我有一个类,它有一个方法,我希望它只能被它的子对象访问,而不能被这个包中的其他类访问。 Modifier | Class | Package | Subclass | World ———————
本文实例讲述了JavaScript中的公有、私有、特权和静态成员用法。分享给大家供大家参考。具体分析如下: 下面的内容是在《JavaScript.DOM高级程序设计》里面摘抄出来的,比较容易理解,
我有一个用例,我已将其简化为以下程序: public class A { private int x = 100; class B { private int y = ne
问题: 类声明如下: class Select { public: template static Iterator function(Iterator , Iterator , bo
我是一名初级 PHP 程序员。我还有很多东西要学。这就是我问这个问题的原因。在一个类中,您有一个公共(public)函数,您可以从该类外部调用它。有时你有一个私有(private)函数,你可以在私有(
问题是: 何时使用私有(private)函数,何时使用嵌套函数? (我在问 F# 但也许答案可能与其他功能语言相关) 一个小例子 namespace SomeName module BinaryRea
我发现工作表中仍然可以使用私有(private)函数。它们是隐藏的,但如果用户输入他们的名字,他们就会被调用。为什么?它应该以这种方式工作吗?有没有办法完全阻止用户定义的函数在 VBA 项目之外使用?
所以我最近开始尝试使用 Kotlin,我偶然发现了这个: If a top-level declaration is marked private, it is private to the pack
我是一名优秀的程序员,十分优秀!