从我的命名空间内访问我的公共方法

发布于 2024-10-10 16:30:22 字数 719 浏览 1 评论 0原文

我正在 JavaScript 中创建自己的命名空间...

(function(window){
    (function(){
        var myNamespace = {
            somePublicMethod: function(){
            },
            anotherPublicMethod: function(){
            }
        }

        return (window.myNamespace = window.my = myNamespace)
    }());
})(window);

我对这些高级 JavaScript 技术很陌生,我正在尝试找出从我的命名空间中调用公共方法的最佳方法。看来在我的公共方法中 this 被设置为 myNamespace

我应该调用公共方法,例如...

AnotherPublicMethod: function(){
   this.somePublicMethod()
}

或...

AnotherPublicMethod: function(){
   my.somePublicMethod();
}

有什么区别吗?

I am in the process of making my own namespace in JavaScript...

(function(window){
    (function(){
        var myNamespace = {
            somePublicMethod: function(){
            },
            anotherPublicMethod: function(){
            }
        }

        return (window.myNamespace = window.my = myNamespace)
    }());
})(window);

I'm new to these kinds of advanced JavaScript techniques and i'm trying to figure out the best way to call public methods from within my namespace. It appears that within my public methods this is being set to myNamespace.

Should I call public methods like...

AnotherPublicMethod: function(){
   this.somePublicMethod()
}

or...

AnotherPublicMethod: function(){
   my.somePublicMethod();
}

is there any difference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不奢求什么 2024-10-17 16:30:22

在我看来,如果您使用 this ,您将使用对该对象的直接引用,而如果您使用 my ,解释器将需要遍历作用域链直到它找到 my 作为 window 的属性。

但反过来也可能存在争论。

编辑:

我应该注意,由于 this 是由函数的调用方式决定的,因此它要求 Activation 对象是该对象。

所以这可行:

my.anotherPublicMethod();

但这不行:

var test = my.anotherPublicMethod;
test();

如果有可能,那么您应该使用 my 或对该对象的其他直接引用。您可以通过维护对对象的引用来减少作用域链遍历。您的 myNamespace 变量应该可以工作。


有点偏离主题,但我还注意到您的代码将无法按原样运行。

此行:

return (window.myNamespace = window.my = myNamespace)

...无权访问 myNamespace 变量。

也许你的意思更像是这样?

(function(window){
    window.myNamespace = window.my = (function(){
        var myNamespace = {
            somePublicMethod: function(){
            },
            anotherPublicMethod: function(){
            }
        }
        return myNamespace;
    }());
})(window);

The way I see it, if you use this you're using a direct reference to the object, whereas if you use my, the interpreter would need to traverse the scope chain until it finds my as a property of window.

But there may be arguments the other way as well.

EDIT:

I should note that since this is determined by how the function is called, it would require that the Activation object be that object.

So this would work:

my.anotherPublicMethod();

But this would not:

var test = my.anotherPublicMethod;
test();

If that's a possibility, then you should use my, or some other direct reference to the object. You could reduce the scope chain traversal by maintaining a reference to the object. Your myNamespace variable should work.


A little off topic, but I'd also note that your code won't work the way it is.

This line:

return (window.myNamespace = window.my = myNamespace)

...doesn't have access to the myNamespace variable.

Perhaps you meant something more like this?

(function(window){
    window.myNamespace = window.my = (function(){
        var myNamespace = {
            somePublicMethod: function(){
            },
            anotherPublicMethod: function(){
            }
        }
        return myNamespace;
    }());
})(window);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文