从公共方法调用的私有方法 [英] Private methods called from public methods

查看:83
本文介绍了从公共方法调用的私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在阅读这篇文章
http://www.crockford.com/javascript/private.html

我想知道是否还有某种方法可以让

可以从公共方法调用的私有方法,但不能从外面的
调用。

如果从外部调用私有方法,则抛出异常

就足够了,但我不知道如何确定该方法是否是从外部调用的
。还有其他人想过这样做吗?


罗伯特。

Hi,
I have been reading the article at
http://www.crockford.com/javascript/private.html
and I was wondering if there also was some way to be able to have
private methods that can be called from public methods, but not from
outside.
Just throwing an exception if a private method was called from outside
would be sufficient, but I don''t know how to determine if the method was
called from outside. Anyone else thought about doing this?

Robert.

推荐答案



Robert写道:

Robert wrote:
我想知道是否还有某种方法能够使用可以从公共方法调用的私有方法,而不是从外面调用。
I was wondering if there also was some way to be able to have
private methods that can be called from public methods, but not from
outside.




有点奇怪的问题。私有方法的整个想法是,他们不能在外面调用
*。如果你可以从外面打电话给方法,那么

它不是私人的。 ;-)


您可以发布一些代码或对象结构,试图实现
实现吗?



A bit strange question. The whole idea of private methods is that they
*cannot* be called outside. If you can call a method from outside, then
it is not private. ;-)

Could you post some code or an object structure you''re trying to
achieve?


VK写道:
Robert写道:
Robert wrote:
我想知道是否还有某种方法可以拥有<可以从公共方法调用的私有方法,但不能从外部调用。

有点奇怪的问题。私有方法的整个想法是它们不能*在外面被调用。如果你可以从外面打电话给方法,那么它不是私密的。 ; - )
I was wondering if there also was some way to be able to have
private methods that can be called from public methods, but not from
outside.

A bit strange question. The whole idea of private methods is that they
*cannot* be called outside. If you can call a method from outside, then
it is not private. ;-)




当然我更喜欢私有方法无法调用,

但我会对错误感到满意。

您可以发布一些您想要实现的代码或对象结构吗?



Of course I would prefer that the private method could not be called,
but I would be satisfied with an error.
Could you post some code or an object structure you''re trying to
achieve?




我在想的代码通常的黑箱公共方法

功能和内部支持功能的私人方法。


例如:


功能列表()

{

this.array = new Array();

}

/ / public

List.prototype.insert = insert;

// private

List.prototype.shiftRight = shiftRight;


函数insert(obj,index)

{

this.shiftRight(index);

this.array [ index] = obj;

}


函数shiftRight(索引)

{

//将数组元素从索引一开始向右移动

}


解决方案离子"在 http://www.crockford.com/javascript/private.html

意味着我必须在List构造函数中移动所有内容。

Insert必须成为特权方法并且shiftRight是私有方法。

我不想将所有内容都移到里面以保持代码更多

清晰易读。如果只有一种方法可以检测到呼叫是来自或外面的

那么我会感到满意。


罗伯特。



The code I am thinking of usual public method for black-box
functionality and private methods for internal support functions.

For example:

function List()
{
this.array = new Array();
}
//public
List.prototype.insert = insert;
//private
List.prototype.shiftRight = shiftRight;

function insert(obj, index)
{
this.shiftRight(index);
this.array[index] = obj;
}

function shiftRight(index)
{
// move array elements starting at index one to the right
}

The "solution" at http://www.crockford.com/javascript/private.html would
mean that I have to move everything inside the List constructor.
Insert must become a privileged method and shiftRight a private method.
I prefer not having to move everything inside to keep the code more
legible. If only a method could detect if the call came from in or
outside then I would be satisfied.

Robert.




Robert写道:

Robert wrote:
代码我正在考虑通常的公共方法,用于黑盒
功能和内部支持的私有方法功能。
The code I am thinking of usual public method for black-box
functionality and private methods for internal support functions.




这就是我使用的方式(请不要我不是任何

类型的真正OOP专家):


< html>

< head>

< title> Untitled Document< / title>

< meta http-equiv =" Content-Type"

content =" text / html; charset = iso-8859-1">

< script>

函数列表(arr){

this.array = arr | | [];

this.count = List.count;

}

List.count = function(){

if(此实例列表){

返回this.array.length;

}

else {

抛出新错误(''私有方法调用范围'');

}

}


var obj =新名单([1,2,3]);

尝试{

alert(obj.count());

alert(列表.count());

}

catch(e){

alert(e.message);

}

< / script>

< / head>


< body>


< / body>

< / html>

还有其他不同的方法和需要的金额

不同的括号。我正在使用这个作为 - 恕我直言 - 最简单的一个作为

它不需要每秒都试图扭曲你的大脑

找出什么& ;这"指向这个或那个地方。


这种方式的缺点是快速模式下的JScript.Net不允许
允许函数对象扩充。因此,如果您计划在JScript.Net上移植一些

解决方案,您最好使用其他方法。来自

的另一方面,JScript.Net有定期的公共方式。和私人修饰符,所以

这样的问题在那里不适用。



This is the way I''m using (please not I''m not a real OOP expert of any
kind):

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>
function List(arr) {
this.array = arr || [];
this.count = List.count;
}
List.count = function() {
if (this instanceof List) {
return this.array.length;
}
else {
throw new Error(''Private method call ourside of scope'');
}
}

var obj = new List([1,2,3]);
try {
alert( obj.count() );
alert( List.count() );
}
catch(e) {
alert(e.message);
}
</script>
</head>

<body>

</body>
</html>
There are other ways different in approaches and needed amount of
different brackets. I''m using this one as - IMHO - the simpliest one as
it doesn''t require to twist your brains every second by trying to
figure out what "this" is pointing to in this or that place.

The drawback of this way is that JScript.Net in quick mode doesn''t
allow Function object augmentation. So if you plan to port some your
solutions on JScript.Net, you may better use something else. From the
other side JScript.Net has regular "public" and "private" modifiers, so
the problem as such is not applicable there.


这篇关于从公共方法调用的私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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