调用Javascript函数时,如何设置自定义值“this”?

时间:2022-12-29 15:50:27

I'm using jQuery and I have a function that serves as an event callback, and so in that function "this" represents the object that that captured the event. However, there's an instance where I want to call the function explicitly from another function - how do I set what "this" will equal within the function in this case?

我正在使用jQuery,并且我有一个用作事件回调的函数,因此在该函数中“this”表示捕获事件的对象。但是,有一个实例,我想从另一个函数显式调用该函数 - 在这种情况下如何设置函数中的“this”将相等?

For example:

function handleEvent(event) {
    $(this).removeClass("sad").addClass("happy");
}

$("a.sad").click(handleEvent); // in this case, "this" is the anchor clicked

function differentEvent(event) {
    $("input.sad").keydown(e) {
        doSomeOtherProcessing();
        handleEvent(e); // in this case, "this" will be the window object
                        // but I'd like to set it to be, say, the input in question
    }
}

5 个解决方案

#1


Use apply call.

使用应用电话。

handleEvent.call(this, e);

#2


Just parameterize the function you're interested in:

只需参数化您感兴趣的功能:

function doStuff(el) {
    $(el).removeClass("sad").addClass("happy");
}

function handleEvent(event) {
    doStuff(this);
}

$("a.sad").click(handleEvent); // in this case, "this" is the anchor clicked

function differentEvent(event) {
    $("input.sad").keydown(e) {
        doSomeOtherProcessing();
        doStuff(this);
    }
}

#3


Use

e.target

#4


I'd advice you re-factoring your function as a jQuery plugin.

我建议你将你的函数重新分解为jQuery插件。

But here's a quick Fix:

但这是一个快速修复:

handleEvent.apply(this,e) //transfers this from one scope, to another

#5


If you're simply looking to call a single event handler as if it were being triggered normally, apply/call will work fine. However, depending on your needs, it may be more robust to use the zero-argument version of jQuery's click() function, which will trigger all click handlers for that element:

如果您只是想将单个事件处理程序调用为正常触发,则apply / call将正常工作。但是,根据您的需要,使用jQuery的click()函数的零参数版本可能会更健壮,这将触发该元素的所有单击处理程序:

function differentEvent(event) {
    $("input.sad").keydown(e) {
        doSomeOtherProcessing();
        $(this).click(); // simulate a click
    }
}

#1


Use apply call.

使用应用电话。

handleEvent.call(this, e);

#2


Just parameterize the function you're interested in:

只需参数化您感兴趣的功能:

function doStuff(el) {
    $(el).removeClass("sad").addClass("happy");
}

function handleEvent(event) {
    doStuff(this);
}

$("a.sad").click(handleEvent); // in this case, "this" is the anchor clicked

function differentEvent(event) {
    $("input.sad").keydown(e) {
        doSomeOtherProcessing();
        doStuff(this);
    }
}

#3


Use

e.target

#4


I'd advice you re-factoring your function as a jQuery plugin.

我建议你将你的函数重新分解为jQuery插件。

But here's a quick Fix:

但这是一个快速修复:

handleEvent.apply(this,e) //transfers this from one scope, to another

#5


If you're simply looking to call a single event handler as if it were being triggered normally, apply/call will work fine. However, depending on your needs, it may be more robust to use the zero-argument version of jQuery's click() function, which will trigger all click handlers for that element:

如果您只是想将单个事件处理程序调用为正常触发,则apply / call将正常工作。但是,根据您的需要,使用jQuery的click()函数的零参数版本可能会更健壮,这将触发该元素的所有单击处理程序:

function differentEvent(event) {
    $("input.sad").keydown(e) {
        doSomeOtherProcessing();
        $(this).click(); // simulate a click
    }
}