document.onkeydown.toString() returns onkeydown function but I only want to get this function’s name.
Is it possible?
var exHandler;
function getCurrentHandler(){
currentHandler = document.onkeydownhandler.toString() - i want to get name of this to call setPreviousHandler with exHandler
document.onkeydown = newFunction;
setPreviousHandler(exHandler);
}
function setPreviousHandler(targetHandler){
document.onkeydown = targetHandler;
}
It depends on the way the function was assigned.
Take look at this:
output is
function test() { alert('test'); }and you can parse this string to get its name as string:(it will allways returns name from function definition (when you assing this function to other variable
.toString()always returns the same string)but following example:
will print
function () { alert('test'); }because this function is anonymous, so you can’t get its name.Moreover when you create function like this:
and output will be
function () { alert('test'); }because on creation function did not have name (it was function expression assigned to variable) so you also can not extract it’s name.But you can make simple comparision:
But there is also issue with this, because when you assign anonymous function like this:
You don’t have any other reference to this function (it is not assigned to other variable) so you can’t compare it with other function (comparision always returns false).