Simple as that, can we emulate the "protected" visibility in Javascript somehow?
很简单,我们可以在某种程度上模仿Javascript中的“受保护”可见性吗?
4 个解决方案
#1
Do this:
/* Note: Do not break/touch this object */
...code...
Or a bit of google found this on the first page:
或者谷歌在第一页上发现了这一点:
http://blog.blanquera.com/2009/03/javascript-protected-methods-and.html
#3
What could that possibly mean? You don't have classes.
那可能意味着什么?你没有课程。
I suppose you could analyze caller
to determine whether it meets some set of criteria for being permitted to call a method. This will be hideously inefficient and your criteria will always be spoofable.
我想你可以分析调用者,以确定它是否符合允许调用方法的一些标准。这将是非常低效的,您的标准将永远是可欺骗的。
#4
There's an interesting pattern worth mentioning here: a JavaScript contructor function may return any object (not necesserily this). One could create a constructor function, that returns a proxy object, that contains proxy methods to the "real" methods of the "real" instance object. This may sound complicated, but it is not; here is a code snippet:
这里有一个值得一提的有趣模式:JavaScript构造函数可以返回任何对象(不一定是这个)。可以创建一个构造函数,它返回一个代理对象,该代理对象包含“真实”实例对象的“真实”方法的代理方法。这可能听起来很复杂,但事实并非如此;这是一段代码:
var MyClass = function() {
var instanceObj = this;
var proxyObj = {
myPublicMethod: function() {
return instanceObj.myPublicMethod.apply(instanceObj, arguments);
}
}
return proxyObj;
};
MyClass.prototype = {
_myPrivateMethod: function() {
...
},
myPublicMethod: function() {
...
}
};
The nice thing is that the proxy creation can be automated, if we define a convention for naming the protected methods. I created a little library that does exactly this: http://idya.github.com/oolib/
好的一点是,如果我们定义一个命名受保护方法的约定,代理创建可以自动化。我创建了一个小图书馆来完成这个:http://idya.github.com/oolib/
#1
Do this:
/* Note: Do not break/touch this object */
...code...
Or a bit of google found this on the first page:
或者谷歌在第一页上发现了这一点:
http://blog.blanquera.com/2009/03/javascript-protected-methods-and.html
#2
#3
What could that possibly mean? You don't have classes.
那可能意味着什么?你没有课程。
I suppose you could analyze caller
to determine whether it meets some set of criteria for being permitted to call a method. This will be hideously inefficient and your criteria will always be spoofable.
我想你可以分析调用者,以确定它是否符合允许调用方法的一些标准。这将是非常低效的,您的标准将永远是可欺骗的。
#4
There's an interesting pattern worth mentioning here: a JavaScript contructor function may return any object (not necesserily this). One could create a constructor function, that returns a proxy object, that contains proxy methods to the "real" methods of the "real" instance object. This may sound complicated, but it is not; here is a code snippet:
这里有一个值得一提的有趣模式:JavaScript构造函数可以返回任何对象(不一定是这个)。可以创建一个构造函数,它返回一个代理对象,该代理对象包含“真实”实例对象的“真实”方法的代理方法。这可能听起来很复杂,但事实并非如此;这是一段代码:
var MyClass = function() {
var instanceObj = this;
var proxyObj = {
myPublicMethod: function() {
return instanceObj.myPublicMethod.apply(instanceObj, arguments);
}
}
return proxyObj;
};
MyClass.prototype = {
_myPrivateMethod: function() {
...
},
myPublicMethod: function() {
...
}
};
The nice thing is that the proxy creation can be automated, if we define a convention for naming the protected methods. I created a little library that does exactly this: http://idya.github.com/oolib/
好的一点是,如果我们定义一个命名受保护方法的约定,代理创建可以自动化。我创建了一个小图书馆来完成这个:http://idya.github.com/oolib/