Search code examples
javascriptparametersevent-bubbling

The 'e' parameter in function's prototype method doesn't work


I want use e parameter to stop javascript bubble

here is my code:

function test(){}
test.prototype.method=function(parameter,e){
    console.log(parameter)
    e.preventDefault();
}
var t=new test();

$(function(){
    $('.node').click(function(){
       t.method($(this).attr('id'),e);
    });
});

however,it doesn't work,the firebug tell me the "e is not defined"

How can I solve this problem?


Solution

  • At the call site

    t.method($(this).attr('id'),e);
    

    there is no symbol named e in scope; therefore, the browser complains. Since symbols do not magically appear, you want to tell the browser that e is the first argument passed in your click handler:

    $('.node').click(function(e){ // ADDED FORMAL PARAMETER
       t.method($(this).attr('id'),e);
    });
    

    You know that there will be a parameter passed to the handler because the docs say that your handler will be given an eventObject -- but of course you are free to name the parameter as you prefer.