“这个”的问题在 JavaScript 命名空间和事件监听器中
首先,我尝试在我的 JavaScript 程序中使用人造命名空间,如下所示:
// Ish.Com namespace declaration
var Ish = Ish || {};
Ish.Com = Ish.Com || {};
// begin Ish.Com.View namespace
Ish.Com.View = new function() {
var privateVariable;
this.publicFunction = function() {
this.publicFunction2()
};
this.publicFunction2 = function() { ... };
};
我并不热衷于使用 this
来调用其他函数,但直到最近,它已经起作用了。不过,我已向某些元素添加了事件侦听器,它们将 this
解释为目标对象。
我知道我可以使用完整的命名空间而不是 this
来调用侦听器内部的函数 (Ish.Com.View.publicFunction2()
),但侦听器经常调用一个函数,它调用另一个函数,另一个函数。我需要在几乎每个函数调用中使用整个命名空间。
如何让命名空间与事件侦听器良好配合? 我也对实现命名空间的更好方法感兴趣,因为使用 this.publicFunction2()
很笨拙。
我对最佳实践非常感兴趣,并学习如何用 JavaScript 编写架构良好的应用程序。然而,在我对 JavaScript 有了更彻底的了解之前,框架是不可能的。
First of all, I'm attempting to use faux namespaces in my JavaScript program like so:
// Ish.Com namespace declaration
var Ish = Ish || {};
Ish.Com = Ish.Com || {};
// begin Ish.Com.View namespace
Ish.Com.View = new function() {
var privateVariable;
this.publicFunction = function() {
this.publicFunction2()
};
this.publicFunction2 = function() { ... };
};
I'm not crazy about using this
to call other functions, but up to recently, it has worked. However, I've added event listeners to some elements, and they interpret this
to be the target object.
I know I can use the full namespace instead of this
to call functions inside of my listeners (Ish.Com.View.publicFunction2()
), but the listeners often call one function, which calls another, and another. I'd need to use the entire namespace in nearly every function call.
How can I get namespaces to work nicely with Event Listeners? I'd also be interested in a better way of implementing namespaces, since using this.publicFunction2()
is clunky.
I'm very interested in best-practices, and learning how to write a well architected application in JavaScript. However, frameworks are out of the question until I gain a more thorough understanding of JavaScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来我今天早上一直以同样的方式回答每个问题:-)
你可以使用“.bind()”:
这将保证
this
每当“eventHandler”时都会引用“yourObject” " 被任何东西调用。在较新的浏览器中,“bind()”函数位于 Function.prototype 对象上。 Mozilla 文档 包含“bind()”的可靠实现您可以使用它来修补旧版浏览器。
“bind()”的作用是返回一个新函数,该函数显式安排
this
按照您的规定进行绑定。如果您愿意,还可以传递要传入的参数。使用“bind()”的另一种方法是将函数调用包装在您自己的匿名函数中:Seems like I've been answering every question this morning the same way :-)
You can use ".bind()":
That'll guarantee that
this
will refer to "yourObject" whenever the "eventHandler" is called by anything.The "bind()" function is there on the Function.prototype object in newer browsers. The Mozilla docs include a solid implementation of "bind()" you can use to patch older browsers.
What "bind()" does is return you a new function that explicitly arranges for
this
to be bound as you stipulate. You can also pass arguments to be passed in, if you like. An alternative to using "bind()" is to wrap the function call in your own anonymous function:我不知道我是否完全理解了你的问题。
这适合您的需求吗?
I don't know if i have understood completely your question.
Would this suit your needs ?
这是由变量上下文和闭包引起的。 “this”始终引用当前对象。事件函数本身就是一个对象,“this”引用该对象。如果您需要引用父对象,可以按照前面所述使用绑定,或者将父对象“this”设置为变量并在事件函数中使用它。
This is caused by variable context and closure. "this" always refers to the current object. The event function is an object itself and "this" refers to that object. If you need to refer to the parent object you can use bind as described previously or you set the parent "this" to a variable and use that in your event function.