为什么这些方法是公开的?

发布于 2024-08-31 20:09:21 字数 289 浏览 5 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迷爱 2024-09-07 20:09:21

拥有私有方法的唯一有效方法是使用闭包。

function MyClass() {
    var privateMethod = function() {
        return 0;
    };

    this.publicMethod = function() {
        return privateMethod();
    };
}

The only effective way to have private methods is by using a closure.

function MyClass() {
    var privateMethod = function() {
        return 0;
    };

    this.publicMethod = function() {
        return privateMethod();
    };
}
心的憧憬 2024-09-07 20:09:21

您将 SayHello 函数添加到传入的对象中,即 One.ABC。你还期待什么?如果您想要一个私有函数,请在匿名函数 (var SayHello = function(){...}) 中定义它,而不将其添加到对象中。不确定您想要完成什么...

编辑

以下是我将如何重写您的代码以完成我认为您想要的事情:

One = {};
One.ABC = {};

(function(ABC) {
    var PrivateHello = function(){
        alert('hello');
    };
    ABC.PublicHello = function() {
        PrivateHello();
    };
})(One.ABC);

One.ABC.PublicHello(); // alerts 'hello'
One.ABC.PrivateHello(); // error 'One.ABC.PrivateHello is not a function'

You are adding the SayHello function into the object being passed in, which is One.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 = {};

(function(ABC) {
    var PrivateHello = function(){
        alert('hello');
    };
    ABC.PublicHello = function() {
        PrivateHello();
    };
})(One.ABC);

One.ABC.PublicHello(); // alerts 'hello'
One.ABC.PrivateHello(); // error 'One.ABC.PrivateHello is not a function'
茶色山野 2024-09-07 20:09:21

您的代码也可以编写为:

var One = {
      ABC:{
       SayHello: function() {
         alert('hello');
       }
      }
};
One.ABC.SayHello(); //=> hello

此变量定义创建一个(伪)命名空间 One (实际上,全局命名空间中的一个对象)。 One 的第一个属性是 ABCABC 也是一个对象,并且有一个属性,即公共方法 SayHello
如果您希望 SayHello 是私有的,这可能是一种方法:

var Two = {
    ABC: ( function(){
        // SayHello is a private function within this
        // anonymous function 
        // it can only be accessed by a public method 
        // you create (here it's the method Hi)
        function SayHello() {
          alert('hello from Two.ABC');
        }
        return {
                SayHello: function(){alert('you are not allowed to say Hello!');},
                Hi: SayHello
               };
        } )()
    }
    Two.ABC.SayHello(); //=> you are not allowed to say Hello!
    Two.ABC.Hi(); //=> hello from Two.ABC

现在,Two.ABC 也是一个对象,但它是使用实例化的匿名构造函数创建的关于创建(我认为它被称为单例模式)。在该构造函数中,SayHello 现在是一个私有(不可公开访问)函数。您必须分配一些公共方法来访问 SayHello (此处:Two.ABC.Hi),否则它将完全隐藏。在此示例中,由于 SayHello 是在匿名函数作用域内定义的,因此匿名函数返回的方法可以访问它,而父作用域(ABC 和 TWo)又可以访问这些方法。换句话说,函数 SayHello 被单例返回的方法包围

Your code can also be written as:

var One = {
      ABC:{
       SayHello: function() {
         alert('hello');
       }
      }
};
One.ABC.SayHello(); //=> hello

This variable definition creates a (pseudo) namespace One (actually, an Object in the global namespace). The first property of One is ABC. 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:

var Two = {
    ABC: ( function(){
        // SayHello is a private function within this
        // anonymous function 
        // it can only be accessed by a public method 
        // you create (here it's the method Hi)
        function SayHello() {
          alert('hello from Two.ABC');
        }
        return {
                SayHello: function(){alert('you are not allowed to say Hello!');},
                Hi: SayHello
               };
        } )()
    }
    Two.ABC.SayHello(); //=> you are not allowed to say Hello!
    Two.ABC.Hi(); //=> hello from Two.ABC

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 constructor SayHello is now a private (not publicly accessible) function. You'll have to assign some public method to access SayHello (here: Two.ABC.Hi), otherwise it will be completely hidden. In this example, because SayHello 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 function SayHello is enclosed by the methods the singleton returns.

智商已欠费 2024-09-07 20:09:21

我希望函数的名称和命名空间的名称相同,这样它们就可以称为 public

I hope the function's name and the Name space's name are same, so those can be referred as public

满意归宿 2024-09-07 20:09:21

RegisterNamespace 不是标准的 JS。但看起来它正在创建一个具有“ABC”属性的对象“One”

匿名函数通过属性“ABC”将其内部函数绑定到对象“One”

所以你最终会得到:

var One = {"ABC" : { "SayHello" : function(){alert('hello')}}}

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:

var One = {"ABC" : { "SayHello" : function(){alert('hello')}}}
小兔几 2024-09-07 20:09:21

对象的所有属性/对象都是公共的。您发布的代码示例与此处相同

Something.RegisterNamespace("One.ABC");
One.ABC.SayHello = function() { alert('hello'); };

,您将属性 SayHello 定义为一个在调用时执行警报语句的函数。

编辑:也许您对代码的结构感到困惑?本节

(function(ABC) {
  ABC.SayHello = function() {
         alert('hello');

  };

})(One.ABC);

相当于,

function addHello(pObject) {
    pObject.SayHello = function() {
        alert('hello');
    };
}

addHello(One.ABC);

唯一的区别是在您的示例中,该函数是内联定义的,然后立即运行。

(...)(parameters);

内联定义它只是使该函数可供一次性使用,在我的示例中,您可以使用相同的函数为许多对象定义一个 SayHello 方法。

all properties/objects of an object are public. the code sample you've posted is equivalent to

Something.RegisterNamespace("One.ABC");
One.ABC.SayHello = function() { alert('hello'); };

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

(function(ABC) {
  ABC.SayHello = function() {
         alert('hello');

  };

})(One.ABC);

is equivalent to

function addHello(pObject) {
    pObject.SayHello = function() {
        alert('hello');
    };
}

addHello(One.ABC);

the only difference is that in your example, the function is being defined inline and then run immediately.

(...)(parameters);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文