简体   繁体   English

Javascript:如何从事件回调函数访问对象成员

[英]Javascript: How to access object member from event callback function

I have some problems trying to figure out what is wrong with my object design. 我试图弄清楚我的对象设计有什么问题,我遇到了一些问题。

var comment = function(){
var textarea = null;
$(document).ready( init );

function init()
{
    $('.reply').click( comment.reply_to );
    this.textarea = $('.commentbox textarea');
    console.log( this.textarea );   // properly shows the textarea element
    console.log( textarea );        // outputs null
}

function set_text( the_text )
{
    console.log( textarea );        // outputs null
    console.log( this.textarea );   // outputs undefined
    textarea.val( the_text );
}

return {
    reply_to: function()
    {
        console.log( this );            // outputs the element who fired the event
        set_text( 'a test text' );      // properly gets called.
    }
};
}();

When document is fully loaded, init() is automatically called and initializes the object. 文档完全加载后,会自动调用init()并初始化对象。 I must note that the textarea member properly points to the desired element. 我必须注意textarea成员正确地指向所需的元素。 A click event is attached to a "reply" button, so reply_to() function is called whenever user clicks on it. 点击事件附加到“回复”按钮,因此只要用户点击它就会调用reply_to()函数。

So, this is what I don't understand: * When using "this" is safe? 所以,这是我不明白的:*使用“这个”是安全的吗? Using it from reply_to() it is not, as it seems like the context is set to the caller element. 使用来自reply_to()的它不是,因为看起来上下文被设置为调用者元素。 * Why I can call "set_text()" from reply_to, but I cannot access the "textarea" member? *为什么我可以从reply_to调用“set_text()”,但我无法访问“textarea”成员? * How I should do to access "textarea" member from reply_to() (which is an event callback)? *如何从reply_to()(这是一个事件回调)访问“textarea”成员?

Since inside those handlers the context will change, it's easiest to keep a reference to the context you want, I personally prefer self . 由于在这些处理程序中,上下文会发生变化,因此最容易保留对所需上下文的引用,我个人更喜欢self Here's one alternative format: 这是一种替代格式:

var comment = function(){
    this.textarea = null;
    var self = this;
    $(document).ready( init );

    function init()
    {
        $('.reply').click( reply_to );
        self.textarea = $('.commentbox textarea');
    }

    function set_text( the_text )
    {
        self.textarea.val( the_text );
    }

    function reply_to() {
      set_text( 'a test text' );
    }
}();

You can test it here . 你可以在这里测试一下 Admittedly though I'm not really sure what you're trying to accomplish though. 诚然,虽然我真的不知道你想虽然完成的任务。 You are trying to return the reply_to function, but bind it yourself inside the init() ready handler...so you can either bind it immediately (like above), or change it up and return what you want to bind elsewhere. 您正在尝试返回reply_to函数,但是在init()就绪处理程序中自己绑定它...因此您可以立即绑定它(如上所述),或者更改它并返回您想要绑定到别处的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Ecmascript 6(ES6)上的类对象中的事件回调函数访问对象成员 - How to access an object member from event callback function in a class object on Ecmascript 6 (ES6) 对象成员函数的回调? (JavaScript)的 - Callback of a object member function? (Javascript) 从javascript访问java对象的成员函数 - Access of a member function of java object from javascript 如何从回调函数访问对象? - How to access an object from a callback function? 如何将事件侦听器回调设置为成员函数 - How to set an event listener callback to a member function 如何访问 JavaScript 事件回调函数内的变量? - How to access variables that are inside a callback function of a JavaScript event? Javascript-如何在事件函数中访问对象属性? - Javascript - how to access object attribute in an event function? 如何从事件监听回调 function 访问 vue3 方法 - How to access vue3 methods from event listener callback function 如何从回调中访问成员变量? - how to access member variables from a callback? 如何传递对拥有成员函数的对象的引用,在该成员函数中将回调注册到JS中的回调? - How to pass a reference to the object owning the member function from within which a callback was registered to the callback in JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM