Possible Duplicate:
If Javascript has first-class functions, why doesn’t this work?
When I try to make an alias function for document.getElementById as below:
f = document.getElementById;
But, when I try to call:
var e_fullname = f(“fullname”);
It was rised an error: Could not convert JavaScript argument
And below is OK:
var e_fullname = f.call(document, “funname”);
Can you tell me why?
There are four ways of calling a function:
f(p1, p2)obj.f(p1, p2)f.apply(obj, [p1, p2]),f.call(obj, p1, p2)new f(p1, p2)In all these cases,
fis just a reference (pointer) to a function object (an object with a[[Call]]internal property). What makes it behave different in all these cases is the way the function is called, and that matters a lot.So,
fis just a reference to thegetElementByIdobject, there’s no difference betweendocument.getElementByIdandsomeOtherHTMLElement.getElementById; the function doesn’t hold back a reference to the object that references it.If you want to bind a certain “owner” object, use the
bindmethod: