提及“这个”是指“这个”。在 javascript 的父闭包中

发布于 2024-09-29 05:19:50 字数 525 浏览 3 评论 0原文

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

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

发布评论

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

评论(3

韬韬不绝 2024-10-06 05:19:50

保留对父级的引用(就像您一样)是一个很好的方法,但是对于您的特定示例,不需要匿名包装器,您可以直接传递函数,如下所示

var self = this;
this.b = function()
{
  Z(self.c);
}

: net/nick_craver/EvZvp/" rel="noreferrer">你可以在这里测试一下,如果没有这个包装器,实际上不需要 self 变量,你可以只使用 直接,如下所示:

this.b = function()
{
  Z(this.c);
}

您可以在此处测试该版本


由于下面的注释似乎有些混乱,所以上面的代码为问题维护了this,如果你想维护this/回调内的上下文,使用 .call()像这样

this.b = function()
{
  Z.call(this, this.c);
}

对于 Z

function Z( f )
{
  f.call(this);
}

您可以在此处进行测试

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:

var self = this;
this.b = function()
{
  Z(self.c);
}

You can test it out here, and without this wrapper there's actually no need for the self variable, you can just use this directly, like this:

this.b = function()
{
  Z(this.c);
}

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 the this/context inside the callback as well, use .call() like this:

this.b = function()
{
  Z.call(this, this.c);
}

And for Z:

function Z( f )
{
  f.call(this);
}

You can test it here.

找个人就嫁了吧 2024-10-06 05:19:50

您也可以使用

this.b = function()
{
    Z( (function () { this.c() }).apply(this) );
}

You can alternatively use

this.b = function()
{
    Z( (function () { this.c() }).apply(this) );
}
栀子花开つ 2024-10-06 05:19:50

有一种通常称为“委托”的模式可以解决这个问题。

在 javascript 中,一个不太花哨的实现可能看起来像这样:

/** class Delegate **/
var Delegate = function(thisRef, funcRef, argsArray) {
    this.thisRef=thisRef;
    this.funcRef=funcRef;
    this.argsArray=argsArray;
}
Delegate.prototype.invoke = function() {
    this.funcRef.apply(this.thisRef, this.argsArray);
}
/** static function Delegate.create - convenience function **/
Delegate.create = function(thisRef, funcRef, argsArray) {
    var d = new Delegate(thisRef, funcRef, argsArray);
    return function() {  d.invoke(); }
}

在您的示例中,您将像这样使用它:

this.b = function() {
  Z( Delegate.create(this, this.c) );
}

您还可以编写期望接收委托的函数:

function Z( d ) {
    d.invoke();
}

然后,在 A 中,您的 b 实现变为:

this.b = function() {
    var d = new Delegate(this, this.c);

    Z( d );
    SomeOtherFunc( d );
}

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:

/** class Delegate **/
var Delegate = function(thisRef, funcRef, argsArray) {
    this.thisRef=thisRef;
    this.funcRef=funcRef;
    this.argsArray=argsArray;
}
Delegate.prototype.invoke = function() {
    this.funcRef.apply(this.thisRef, this.argsArray);
}
/** static function Delegate.create - convenience function **/
Delegate.create = function(thisRef, funcRef, argsArray) {
    var d = new Delegate(thisRef, funcRef, argsArray);
    return function() {  d.invoke(); }
}

In your example, you would use it like this:

this.b = function() {
  Z( Delegate.create(this, this.c) );
}

you could also write functions that expect to receive a Delegate:

function Z( d ) {
    d.invoke();
}

then, in A, your impl of b becomes:

this.b = function() {
    var d = new Delegate(this, this.c);

    Z( d );
    SomeOtherFunc( d );
}

The Delegate just provides a simple, consistent way of encapsulating the this reference (which you've called self), 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 like self. 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).

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