为什么这些方法是公开的?
我的 JavaScript 如下所示。我不明白为什么这些方法都是公开的?
Something.RegisterNamespace("One.ABC");
(function(ABC) {
ABC.SayHello = function() {
alert('hello');
};
})(One.ABC);
所以现在我可以这样做:
One.ABC.SayHello();
My javascript looks like the following. I don't understand why these methods are all public though?
Something.RegisterNamespace("One.ABC");
(function(ABC) {
ABC.SayHello = function() {
alert('hello');
};
})(One.ABC);
So now I can do:
One.ABC.SayHello();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
拥有私有方法的唯一有效方法是使用闭包。
The only effective way to have private methods is by using a closure.
您将
SayHello
函数添加到传入的对象中,即One.ABC
。你还期待什么?如果您想要一个私有函数,请在匿名函数 (var SayHello = function(){...}
) 中定义它,而不将其添加到对象中。不确定您想要完成什么...编辑:
以下是我将如何重写您的代码以完成我认为您想要的事情:
You are adding the
SayHello
function into the object being passed in, which isOne.ABC
. What else are you expecting? If you want a private function, define it inside your anonymous function (var SayHello = function(){...}
) without adding it to the object. Not sure what you're trying to accomplish...EDIT:
Here's how I would rewrite your code to do what I think you want:
您的代码也可以编写为:
此变量定义创建一个(伪)命名空间
One
(实际上,全局命名空间中的一个对象)。One
的第一个属性是ABC
。ABC
也是一个对象,并且有一个属性,即公共方法SayHello
。如果您希望 SayHello 是私有的,这可能是一种方法:
现在,Two.ABC 也是一个对象,但它是使用实例化的匿名构造函数创建的关于创建(我认为它被称为单例模式)。在该构造函数中,
SayHello
现在是一个私有(不可公开访问)函数。您必须分配一些公共方法来访问SayHello
(此处:Two.ABC.Hi
),否则它将完全隐藏。在此示例中,由于SayHello
是在匿名函数作用域内定义的,因此匿名函数返回的方法可以访问它,而父作用域(ABC 和 TWo)又可以访问这些方法。换句话说,函数SayHello
被单例返回的方法包围。Your code can also be written as:
This variable definition creates a (pseudo) namespace
One
(actually, an Object in the global namespace). The first property ofOne
isABC
.ABC
is an Object too and has one property, the public methodSayHello
.If you wanted
SayHello
to be private, this could be a way to do it:Now
Two.ABC
is an object too, but it is created using an anonymous constructor function instantiated on creation (a singleton pattern I think it's called). Within that constructorSayHello
is now a private (not publicly accessible) function. You'll have to assign some public method to accessSayHello
(here:Two.ABC.Hi
), otherwise it will be completely hidden. In this example, becauseSayHello
is defined within the anonymous function scope, it is accessible for the methods that anonymous function returns, which in turn are accessible to the parent scopes (ABC and TWo). In other words, the functionSayHello
is enclosed by the methods the singleton returns.我希望函数的名称和命名空间的名称相同,这样它们就可以称为 public
I hope the function's name and the Name space's name are same, so those can be referred as public
RegisterNamespace 不是标准的 JS。但看起来它正在创建一个具有“ABC”属性的对象“One”
匿名函数通过属性“ABC”将其内部函数绑定到对象“One”
所以你最终会得到:
RegisterNamespace is not standard JS. But that looks like it is creating an object "One" with a property of "ABC"
The anonymous function is binding its inner function to the object "One" via the property "ABC"
So you end up with:
对象的所有属性/对象都是公共的。您发布的代码示例与此处相同
,您将属性
SayHello
定义为一个在调用时执行警报语句的函数。编辑:也许您对代码的结构感到困惑?本节
相当于,
唯一的区别是在您的示例中,该函数是内联定义的,然后立即运行。
内联定义它只是使该函数可供一次性使用,在我的示例中,您可以使用相同的函数为许多对象定义一个
SayHello
方法。all properties/objects of an object are public. the code sample you've posted is equivalent to
here, you're defining the property
SayHello
to be a function that executes the alert statement when called.edit: perhaps you're being thrown off by the structure of the code? this section
is equivalent to
the only difference is that in your example, the function is being defined inline and then run immediately.
defining it inline just make the function available for one-off use, where in my example you could use the same function to define a
SayHello
method for many objects.