-2

Can somebody please explain the pros and cons for below.

Am having a function to get the url querystring parameters, but I need to know which is the best way to write the function. Eg: if i create the function using jquery plugin style, then every time I need to use a target element to access the function as below

$("#targetDom").getQueryString("name");

However, if I create the function using javascript classes or javascript design pattern, it would be

getQueryString("name");

This is a small example but considering large application which approach is best? is there any disadvantage in going with jquery plugin way?

Shef
  • 44,808
  • 15
  • 79
  • 90
Navin Leon
  • 1,136
  • 3
  • 22
  • 44
  • May I ask why you aren't using one of the numerous plugins / functions already available to do this? See [this post](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values) for tons of options. Also, you don't need a selector to use a jQuery plugin, you could extend it and use `$.fn.getQueryString("name");` (for example). – Cᴏʀʏ Feb 28 '13 at 18:38
  • 1
    duplicate of [Jquery plugin Vs javascript](http://stackoverflow.com/questions/15142574/jquery-plugin-vs-javascript) - which was closed because this is a question without a specific and definite answer, and thus [is not suitable for StackOverflow.](http://stackoverflow.com/faq) – Blazemonger Feb 28 '13 at 18:39
  • Write a jQuery plugin when you specifically want to do something with jQuery collections. – kapa Feb 28 '13 at 18:40
  • @Madbreaks Because there is no definite answer. It is *not constructive*: `As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.` – kapa Feb 28 '13 at 18:42
  • @bažmegakapa am not sure why this is not constructive, because this a question from a developer with real time situation, is this community not help developers each other ? – Navin Leon Feb 28 '13 at 18:46
  • @NavinLeon There is no definitive answer to your question. And this is a Q&A site. Not a forum, where people gather to discuss things and collide opinions. Also, I'm not the one making the rules here, so don't shoot the messenger :). – kapa Feb 28 '13 at 23:12

2 Answers2

2

The answer will depend on who you ask.

My answer is, if you're already using jQuery on your page (meaning leveraging jQuery won't require you to import additional resources) then go ahead and use jQuery to write your extension. jQuery does internal chaching which I believe can actually make your code faster than using native JavaScript in some cases.

If you're not already using jQuery, using vanilla JavaScript for your particular task isn't complex enough a problem to require (or even suggest you might want to use) jQuery.

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
1

You don't have to create a jQuery function for every functionality, especially if you are not going to be dealing with the DOM, which seems to be your case.


However, you might want to group such functions in a small library of your own application, which can be reused in other applications as well.

For example:

var utils = {
    getQueryString : function(name){}
};

and access it like this...

utils.getQueryString("name");

Eg: if i create the function using jquery plugin style, then every time I need to use a target element to access the function as below

No, you don't. You can run the function like this as well $.fn.getQueryString("name");.

Shef
  • 44,808
  • 15
  • 79
  • 90