I recently tried to write shortcuts for build-in object functions in JavaScript.
Here it is:
function changeit(){
var ge = getElementsByTagName() , doc = document;
doc.ge('div')[0].innerHTML = 'Changed';
}
I thoughy it would actually work but it didn’t. Is there a way to make it work? I mean calling a built-in object function by a var name, and will it work in all browsers?
The function
getElementsByTagNamedoes not exist barely. You have to access it as a property ofdocument, but even then, you have to save the reference todocumentas well:Or, you could save the name as a string:
Then you can do:
Because
obj['foo']is equal toobj.foo.