lot of times when passing function by reference in javascript usually to events i have to write:
something.onclick = function(){
callback(3);
};
but i rather always want to do this:
something.onclick = callback...put parameters here
so basically i always wanted to pass reference to a function along parameters so it won't get executed right away.
Recently after thinking and looking around i made this..
Function.prototype.pass = function(){
var args=arguments, func = this;
return function(){ func.apply(this,args);}
};
now i can just add callbacks without writing placeholder functions like this:
eleA.onclick(logWhichEle.pass('A'));
eleB.onclick(logWhichEle.pass('B'));
eleC.onclick(logWhichEle.pass('C'));
when i was new i tried to do this even though i knew it won't work i was hopping it would somehow..
eleA.onclick(logWhichEle('A'));
but that just returned undefined.
So is doing solution above unsafe..what can i do further to make it safer. Like not unintentionally break someone's code.