Skip to content

Instantly share code, notes, and snippets.

@scottgonzalez
Created November 13, 2009 02:32
Show Gist options
  • Save scottgonzalez/233531 to your computer and use it in GitHub Desktop.
Save scottgonzalez/233531 to your computer and use it in GitHub Desktop.
jQuery object normalization
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;
}
@rentalhost
Copy link

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 $([]);
}

@fdutey
Copy link

fdutey commented Feb 3, 2011

Why not?

@rentalhost
Copy link

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/

@jzaefferer
Copy link

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

@scottgonzalez
Copy link
Author

FLASH CAGE!

@rentalhost
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment