Can a “shortcut” not be made to methods such as document.createElement, document.createTextNode, [element].setSelectionRange etc?
var c = document.createElement;
var div = c('div');
div.innerHTML = 'blah';
document.body.appendChild(div);
When executing the above code Firebug console returns an error:
uncaught exception: [Exception… “Could not convert JavaScript argument” nsresult: “0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)” location: “JS frame :: http://fiddle.jshell.net/_display/ :: :: line 20″ data: no]
This happens on jsfiddle as provided here and silently fails when done outside of jsfiddle with no errors.
The below code works fine, so is it just limited to DOM manipulation methods?
function long_function_name(prop)
{
alert(prop);
}
var c = long_function_name;
c('blah');
This has practical examples for compression sake:
Instead of:
if (element.setSelectionRange)
{
element.setSelectionRange(pos, pos);
}
Compress to:
var s = element.setSelectionRange || 0;
if (s) s(pos, pos);
There are two obvious issues:
thisvalue (it will be the global object rather thandocument), which the DOM method may or may not depend upon;Functionobject and may not therefore have thecall()orapply()methods that you could otherwise use to provide the correctthisvalue.This being the case, you’re better off writing a wrapper function instead, such as