提及“这个”是指“这个”。在 javascript 的父闭包中
我想在Javascript中做到这一点:
function Z( f )
{
f();
}
function A()
{
this.b = function()
{
Z( function () { this.c() } );
}
this.c = function()
{
alert('hello world!');
}
}
var foo = new A();
foo.b();
可以这样完成:
function Z( f )
{
f();
}
function A()
{
var self = this;
this.b = function()
{
Z( function () { self.c() } );
}
this.c = function()
{
alert('hello world!');
}
}
var foo = new A();
foo.b();
有更好的方法吗?
I want to do this in Javascript:
function Z( f )
{
f();
}
function A()
{
this.b = function()
{
Z( function () { this.c() } );
}
this.c = function()
{
alert('hello world!');
}
}
var foo = new A();
foo.b();
It can be accomplished this way:
function Z( f )
{
f();
}
function A()
{
var self = this;
this.b = function()
{
Z( function () { self.c() } );
}
this.c = function()
{
alert('hello world!');
}
}
var foo = new A();
foo.b();
Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保留对父级的引用(就像您一样)是一个很好的方法,但是对于您的特定示例,不需要匿名包装器,您可以直接传递函数,如下所示
: net/nick_craver/EvZvp/" rel="noreferrer">你可以在这里测试一下,如果没有这个包装器,实际上不需要
self
变量,你可以只使用直接
,如下所示:您可以在此处测试该版本。
由于下面的注释似乎有些混乱,所以上面的代码为问题维护了
this
,如果你想维护this
/回调内的上下文,使用.call()
像这样:
对于
Z
:您可以在此处进行测试。
Keeping a reference to the parent (like you have) is a good approach, however for your specific example there's no need for the anonymous wrapper, you can pass the function directly, like this:
You can test it out here, and without this wrapper there's actually no need for the
self
variable, you can just usethis
directly, like this:You can test that version here.
Since there seems to be some confusion in the comments below, the above code maintains
this
for the question, if you want to maintain thethis
/context inside the callback as well, use.call()
like this:And for
Z
:You can test it here.
您也可以使用
You can alternatively use
有一种通常称为“委托”的模式可以解决这个问题。
在 javascript 中,一个不太花哨的实现可能看起来像这样:
在您的示例中,您将像这样使用它:
您还可以编写期望接收委托的函数:
然后,在
A
中,您的b
实现变为:Delegate
只是提供了一种简单、一致的方式来封装this
引用(您将其称为>self
),在一个对象实例中,可以像任何其他对象实例一样处理它。它更具可读性,并且可以让您不必使用self
等多余的变量来污染函数作用域。更高级的委托实现可以有自己的方法和其他相关状态。还可以以有助于最小化一些与作用域相关的内存管理问题的方式构建委托(尽管我在此处显示的代码绝对不是这样的示例)。There is a pattern that's often called "Delegate", which addresses this issue.
In javascript, a not-too-fancy implementation might look something like this:
In your example, you would use it like this:
you could also write functions that expect to receive a Delegate:
then, in
A
, your impl ofb
becomes:The
Delegate
just provides a simple, consistent way of encapsulating thethis
reference (which you've calledself
), within an object instance that can be dealt with like any other object instance. It's more readable, and it keeps you from having to pollute your function scope with superfluous variables likeself
. A fancier delegate implementation could have its own methods and other related state. It's also possible to build the delegate in such a way that it helps to minimize some scope-related memory management problems (though the code I've shown here is definitely not an example of that).