cujojs / meld

AOP for JS with before, around, on, afterReturning, afterThrowing, after advice, and pointcuts
Other
644 stars 65 forks source link

dom event not working #40

Closed FishOrBear closed 7 years ago

FishOrBear commented 7 years ago
let myObject = {
    f: function clickHandle()
    {
        console.log("click");
    }
}
window["obj"] = myObject;
document.addEventListener("click", myObject.f)
var remover = meld.after(myObject, 'f', () =>
{
    console.log("f after");
});

If you type obj.f () in the console it will trigger the "after" event correctly

it's working

let myObject = {
    f: function clickHandle()
    {
        console.log("click");
    }
}
window["obj"] = myObject;
var remover = meld.after(myObject, 'f', () =>
{
    console.log("f after");
});

document.addEventListener("click", myObject.f)
briancavalier commented 7 years ago

Hi @FishOrBear. You just need to apply the advice first:

let myObject = {
    f: function clickHandle()
    {
        console.log("click");
    }
}
window["obj"] = myObject;
var remover = meld.after(myObject, 'f', () =>
{
    console.log("f after");
});

document.addEventListener("click", myObject.f)

Because functions in JS are immutable, meld advises object methods by overwriting them. That is meld.after(myObject, 'f', advice) basically overwrites myObject['f'] = advice.

Hope that helps.

FishOrBear commented 7 years ago

Thank you