belykh
belykh

Reputation: 1300

Reference of function

I'm trying to make a reference of function in JavaScript to the obfuscate of source code

var g = document.getElementById;

but the call g('id') causes an error in Google Chrome TypeError: Illegal invocation

Upvotes: 1

Views: 61

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382474

The context of the function call must be the document. Use

var g = document.getElementById.bind(document);

If you want to be compatible with IE8 (which doesn't have bind), use

var g = function(id){
    return document.getElementById(id);
}

Upvotes: 5

Related Questions