Manish Mulani
Manish Mulani

Reputation: 7375

jQuery object or div id string as a param?

Which is a better option,

to pass a string representing div id as a parameter to a function and in the function get the jQuery object using $("#" + divid) and update it

or

to pass the jQuery object itself as a parameter to the function ?

Thanks

Upvotes: 1

Views: 768

Answers (2)

Peng Qi
Peng Qi

Reputation: 1462

I prefer the second, passing the object, to reduce the cost of fetching and initialing the object again.

Upvotes: 0

Mutation Person
Mutation Person

Reputation: 30498

I suppose if we're up for considering the Dependency Inversion OO principle, we should pass the object (i.e. inject the dependency), and let the function's code refer to the abstraction (the jQuery object interface). You'll also have saved some overhead from removing some string operations.

Otherwise, I'd not fret too much. The most important thing is to be consistent across your code base.

Upvotes: 1

Related Questions