如何访问 Mootools fireEvent 函数的第二个参数中的对象实例?

发布于 2024-12-21 13:22:48 字数 721 浏览 1 评论 0原文

我正在 Mootools 中使用实现:[事件、选项]

我想将类属性传递给事件触发时调用的函数。我本以为我可以使用 this 来做到这一点,但 this 未定义。

我已经评论了下面我的问题所在的代码。我有一种感觉,这可能与绑定有关,所以我现在就看一下。预先感谢您的任何帮助!

var Person = new Class({

    Implements: [Options, Events],

    initialize: function(options){
        this.setOptions(options);
    }

});

greet = function(name){
    log('Hello, I am ' + name);
}

var ryan = new Person({
    name: 'Ryan',
    onArrive: greet //add event 'arrive'
});
ryan.fireEvent('arrive', ryan.options.name); //ideally I would use this.options.name?

这段代码可以工作,但在最后一行中使用对象实例名称而不是 this 似乎很奇怪。

I'm working through Implements: [Events, Options] in Mootools.

I would like to pass a class property to the function that is called when an event fires. I would have thought that I could do it using this but this is undefined.

I have commented the code below where my problem is. I have a feeling that this might be to do with binding so I will be having a look at that now. Thanks in advance for any help!

var Person = new Class({

    Implements: [Options, Events],

    initialize: function(options){
        this.setOptions(options);
    }

});

greet = function(name){
    log('Hello, I am ' + name);
}

var ryan = new Person({
    name: 'Ryan',
    onArrive: greet //add event 'arrive'
});
ryan.fireEvent('arrive', ryan.options.name); //ideally I would use this.options.name?

This code works but it just seems strange to use the object instance name instead of this in the last line.

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

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

发布评论

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

评论(2

梦中楼上月下 2024-12-28 13:22:48

当您触发事件时,此您的 ryan 实例。
要使其工作,只需进行以下更改

function greet(name){
    log('Hello, I am ' + this.options.name); //<-- use this here 
}

ryan.fireEvent('arrive');

,但由于现在问候语取决于实例,因此最好将其包含在您的类定义中

var Person = new Class({

    Implements: [Options, Events],

    initialize: function(options){
        this.setOptions(options);
    },
    greet: function() { log('Hello, I am ' + this.options.name); //<-- use this here  }
});

,并且

var ryan = new Person({
    name: 'Ryan',
    onArrive: this.greet.bind(this)  //or a function() { /* log('Hello,... */}
});

When you fire the event this is your instance of ryan.
To make it work just make the following changes

function greet(name){
    log('Hello, I am ' + this.options.name); //<-- use this here 
}

ryan.fireEvent('arrive');

but since greet now depends on the instance it would be better to include it in your class definition

var Person = new Class({

    Implements: [Options, Events],

    initialize: function(options){
        this.setOptions(options);
    },
    greet: function() { log('Hello, I am ' + this.options.name); //<-- use this here  }
});

and

var ryan = new Person({
    name: 'Ryan',
    onArrive: this.greet.bind(this)  //or a function() { /* log('Hello,... */}
});
小苏打饼 2024-12-28 13:22:48

this 关键字指的是当前对象。在您的情况下,this 可能指的是全局对象(窗口)。您必须位于 ryan 实例范围内,以便可以将 ryan 引用为 this

function fireEvent(e){
    this.fireEvent(e,this.options.name);
}
fireEvent.call(ryan,'arrive');  

您还可以重载 fireEvent< /code> 方法(如果您不想再次使用对同一实例的引用)。像这样的东西:

var originalFireEvent = Person.prototype.fireEvent;
Person.prototype.fireEvent = function(e,o){
    var a = typeof this.options[o] !== 'undefined' ? this.options[o] : null;
    return originalFireEvent.call(this,e,a);
}

然后这样称呼它:

ryan.fireEvent('arrive','name');

the this keyword refers to the current object. In your case, this probably refers to the global object (window). You have to be in the ryan instance scope so that you can refer to ryan as this :

function fireEvent(e){
    this.fireEvent(e,this.options.name);
}
fireEvent.call(ryan,'arrive');  

You could also overload the fireEvent method if you don't want to use a reference to the same instance again. Something like this :

var originalFireEvent = Person.prototype.fireEvent;
Person.prototype.fireEvent = function(e,o){
    var a = typeof this.options[o] !== 'undefined' ? this.options[o] : null;
    return originalFireEvent.call(this,e,a);
}

and then call it like this :

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