I have a function that expects a jQuery object, but want to also accept selector strings and elements for convenience. So I am doing something like this:
test($('.something'));
test('.something');
test($('.something')[0]);
function test (param1) {
//ensure jquery object
if (!(param1 instanceof $)) {
param1 = $(param1);
}
//do something with $param1
}
Would it hurt to just initialize the jQuery object regardless or are there implications and performance issues with this?
function test (param1) {
//ensure jquery object
param1 = $(param1);
//do something with $param1
}
Both of them will work. But if you are looking for the best option you should go with the first function. Check this out.
Update
Here it is the perf with corrections by the OP and one extra test I didn’t think in.