4

Possible Duplicate:
JavaScript function aliasing doesn't seem to work
Set document.getElementById to variable

Code would be more efficient if this was possible:

var min = document.getElementById; 

and then call document.getElementById() using min().

Not trying to write minified code but in this particular case one can reduce scope lookup and shorten some lines.

Is this a syntax issue or a limiation on the language?

2
  • 1
    Can you use delegate function like var min = function(inID){ return window.document.getElementById(inID); };? Commented May 23, 2012 at 15:59
  • 1
    "Code would be more efficient"? Commented May 23, 2012 at 16:03

1 Answer 1

6

When you call foo.bar() then this is set to foo inside bar When you copy foo.bar to window.bar then call window.bar(), this is set to window.

getElementById has to operate on a DOM document object.

Sign up to request clarification or add additional context in comments.

1 Comment

window is the default object, it is the same question just making window explicit instead of looking up the scope chain.