public
Last active

jQuery object normalization

  • Download Gist
jquery-element.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
function element(value, context) {
var ret = $([]); // $(context) ?
 
if (value.jquery) {
ret = value;
} else if (value == 'parent') {
ret = $(context).parent();
} else if (value == 'clone') {
ret = $(context).clone().removeAttr('id');
} else if (value == 'window') {
ret = $(context).window(); // requires .window() plugin
} else if (value.nodeType || typeof value == 'string' || $.isArray(value)) {
ret = $(value, context);
} else if ($.isFunction(value)) {
ret = value(context);
}
 
return ret;
}

Why not more directly? Example:

function .. {
if(value.jquery) return value;
else if(value === 'parent') return $(context).parent();
else if(value === 'clone') return $(context).clone().removeAttr('id');
else if(value === 'window') return $(context).window();
else ...
return $([]);
}

yeah... on this code, "ret" store a value that is returned on line 18... then, why not return this directly, without store in a variable?
Example: http://jsfiddle.net/XnuuY/

Don't bother optimizing before testing, the code doesn't work anyway.

@jzaefferer haha, I just comment. I don't tried the code, for me are ok. :(

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.