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 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, visit the help center for guidance.
Closed 9 years ago.
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?
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.
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");.
Related
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 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, visit the help center for guidance.
Closed 9 years ago.
I am creating an ajax powered webbased application. I am using jQuery and php. As a temporary solution when $.ajax success callback occurs I load the file.php into the page and on done I run a custom function initialize_js which looks for elements that I know need initialization and runs some javascript for those elements.
My current solution is similiar to this post
IMHO, this is going to get quite large and hard to manage. I know I could separate the js into it's own file and make a 2nd ajax call to load this content as a script but I felt this would have alot of overhead.
What do you recommend so that the code is organized and reusable. I hope to put my javascirpt_ajax routines into a reusable file for other projects on the site and these customized functions cannot be in there.
First of all, I recomand you to write your javascript code as OOP classes/functions. Google will help you with that.
Then since you said you will have pretty much code, is kinda mandatory to split the JS code in multiple files for multiple reasons: flexibility, maintenance, optimisation etc
In the end you should take a look at[ RequireJS. It helps you to load js files on the fly. So you load only the functions you need, when you need. Not all the files at the same time.
P.S. IF you want to be more rigorous and work like a pro then you might try an MVC library like BackboneJS, SpineJS, AngularJS etc.
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 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, visit the help center for guidance.
Closed 10 years ago.
I hope this question is not too general, but i haven't found any good tutorial on how to do the transition from standard javascript / jQuery code to modular / tested code.
Mmy new task is the complete refactoring of the javascript of our wordpress plugin.
Our current code is standard jQuery code, thrown all into a file, with checks on the existance of dom elements to understand on what page we are in and attach the correct event handlers.
My goal is simply to write more manteinable and clear code, and introduce tests in the process.
My approach would be:
Try to modularize code in separate files (one file for each page) and use require.js to load only the required code.
write some tests with jasmine/sinon and try to adapt the esisting code so that the tests pass
Have you got any other suggestions/best practice?
Can you share some example on how you approached similar tasks?
I was thinking about bringing in an mvc framework but if i can stick to standard jQuery code i think it's easier for the development team because it doesn't add complexity
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 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, visit the help center for guidance.
Closed 10 years ago.
Learn JavaScript before learning JQuery?
I have read that thread and I still don't find any points convincing me that Javascript is a must-learn-before-jquery thing.
I think jquery is far more than just a javascript library, I would like to call it a redefinition of the dynamic language, the new javascript instead. The library writers clearly made the greatest hit here to change old JavaScript's face almost completely.
Javascript and Jquery are co-existing because Jquery is the javascript library and the fact that people support learning JS first before jquery is likely because they've been client side coders long before the jquery was out, WHICH CURRENTLY EASES THEIR INVESTIGATIONS INTO THE LANGUAGE FAR BETTER, psychologically, they certainly then say "Oh yes, procedural methodologies work greater".
For points concerning writing a function call via mouse/keyboard events, I still agree that javascript structures are preserved and need learning. But does this really make a big difference whether or not learning jquery first should be more beneficial at all ?
Ex:
function something()
{
//jquery code
}
////
<input type="..." onclick="something();"/>
Advice and corrections are required. Thank you so very much.
There are many reasons why its important to learn the native JavaScript, in addition to the jQuery Library:
jQuery is written in JavaScript, so if you ever run into a jQuery bug, or need to patch it, you will need to know the JavaScript
if you ever want to use jQuery efficiently, in many cases it's helpful to read the underlying JavaScript
it isn't guaranteed that jQuery will always be the best Library (something like Scriptaculous may one day surpass it), thus you shouldn't be library-dependent
in many cases you still need to run JavaScript inside of a jQuery function
jQuery may make many things simpler (accessing DOM elements), but in many cases it isn't required
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 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, visit the help center for guidance.
Closed 10 years ago.
i don't understand the meaning of the word library -
regarding to jQuery: will it be right to say that "library" is a huge file with many plug ins that are ready to use?
jQuery is a fast and concise JavaScript Library that simplifies HTML
document traversing, event handling, animating, and Ajax interactions
for rapid web development. jQuery is designed to change the way that
you write JavaScript.
All this means is that jQuery itself does not do anything. A library is "a collection of resources used to develop software". jQuery allows you to write cross-browser JavaScript a heck of a lot easier than it would be without it.
A library is something that extends a base langage. So in this definition jQuery is a library
A small(not huge!!!) file (32K only)... With many many functions and features.
Libraries contain code and data that provide services to independent programs. This encourages the sharing and changing of code and data in a modular fashion, and eases the distribution of the code and data.
Wikipedia
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 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, visit the help center for guidance.
Closed 9 years ago.
I'm wondering if such a library exists, where the library contains only a collection of common utility functions such as trim, indexOf (for arrays), map, each, typeOf and so on...
I'm aware that jQuery provides some of the functions I listed above, but I'm looking for something that is designed purely for this (I really don't need jQuery on the server-side running node.js for instance, nor do I want to depend on it if I'm writing a non-jQuery-related JavaScript library). I've recently begun collecting an assortment of these functions for future copy/pasting, but my guess is that there are many others that I won't even know to look for, or think of using, unless someone presents them to me.
I'm fond of underscore.js; it doesn't provide string utilities such as trim; it's focused on object-oriented and functional utilities, including all of the other things you mention. Another nice thing about it is that it doesn't reference the DOM at all, so it's useful for javascript programming that isn't web-based or DOM related.
The functions you mention are all standard in ECMAScript 5. And this library implements them in such a way that you can use them in older browsers/JavaScript versions as well, in a way that will be compatible when your environment catches up with the standard:
https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js
Boiler.js is an up and coming utility library that offers many useful JavaScript utilities and is a direct competitor with Underscore.js.
jQuery provides all of those and many more, you would be better off just using it.
jQuery can sit side-by-side with other frameworks, so can be independent if another framework is present.
See: jQuery Utilities Documentation
Javascript itself has many of these functions built into the basic types. Before building your own, perhaps a copy of JavaScript: The Definitive Guide, focusing on the API reference in the back, would do you some solid good.
After that investigate frameworks, or at least being looking into how you can create your own framework for your functions (as opposed to copying and pasting). Here the module pattern would be helpful to you.