- news (347)
- links (345)
- jQuery Plugins (61)
- jQuery Code Snippets (34)
- jQuery and JavaScript (21)
- plugins (17)
- jQuery News (17)
- Uncategorized (16)
- Media (13)
- jQuery Mobile (11)
- jQuery and CSS (10)
- jQuery Utilities (9)
- jQuery and Ajax (9)
- Random jQuery Stuff (9)
- jQuery Functions (8)
- jQuery Articles (8)
- images (7)
- jQuery (7)
- jQuery Tutorials (7)
- jQuery and HTML (6)
- Javascript Libraries (6)
- jQuery and WordPress (6)
- jQuery Events (6)
- jQuery Testing (6)
- jQuery Image Scripts (5)
- APIと少しのjQueryコードでGoogle Mapの地図の色を変更する - かちびと.net
- flipCounter a jQuery plugin by BloggingSquared.ca
- D&DEPARTMENT PROJECT
- iPhoneやAndroid等のタッチデバイス向けイメージギャラリー用JSライブラリ・PhotoSwipeなかなか良いですぞ - かちびと.net
- 画像ギャラリー等、スマートフォン(iPhone / Android)用にフリック・スワイプ操作を実現するjQueryプラグインまとめ | HTML5 – CSS3 mag
- PhotoSwipe
- HTML KickStart - Ultra–Lean HTML Building Blocks for Rapid Website Production - KickStart your Website Production - 99Lime.com
- Tutorials:Zebra Striping Made Easy - jQuery JavaScript Library
- Overview
- Real Shadow: jQuery Plugin that casts photorealistic shadows
- Calling a Script when button clicked - jQuery Forum
- [JS]異なる高さのdiv要素を揃えるだけでなく、均等割付やグリッドに揃えることもできるスクリプト -jQuery.grrrid.js | コリス
- 5 Excellent jQuery Mobile Tutorials | blogfreakz.com
- Jquery.ascensor.js
- Ajax Upload; A file upload script with progress-bar, drag-and-drop.
- jqBootstapValidation by ReactiveRaven
- [5]テーブル、チャート、コード表示プラグインを活用する - みてわかる!jQuery入門:ITpro
- Comparing HTML5 Mobile Web Framework - Dzyngiri
- 20 More CSS3 Tutorials and Techniques for Creating Buttons
- A jQuery plugin to help ease the transition to responsive images | Jquery Picture
- Infragistics jQuery Controls – Impressive & Professional jQuery Toolset
- remybach/jQuery.superLabels - GitHub
- Tips and best practices to develop responsive websites | CatsWhoCode.com
- What’s new for designers, May 2012 | Webdesigner Depot
- dynamo.js
5 Different Ways to Declare Functions in jQuery
Introduction
Choosing which way to declare a JavaScript function can be confusing for beginners as there are several different ways to declare functions using JavaScript/jQuery. I’ll try to explain the benefits of each one and how and why you might use them when writing your awesome jQuery code.
1. The basic JavaScript function
This is the simplest way to declare a function in JavaScript. Say for example, we want to write a simple function called multiply(x,y) which simply takes in two parameters x and y, does a simple x times y and returns the value. Here are a few ways you might go about doing exactly this.
function multiply(x,y) {
return (x * y);
}
console.log(multiply(2,2));
//output: 4
If you wanted a quick function to test something then maybe that’s the only occasion you would use this. It’s not good coding and doesn’t promote code reuse.
2. JavaScript functions for get/set
If you need a private utility for getting/setting/deleting model values then you can declare a function as a variable like this. This could be useful for assigning a variable upon declaration calculated by a function.
var multiply = function(x,y) {
return (x * y);
}
console.log(multiply(2,2));
//output: 4
//The same function but with a self execution to set the value of the variable:
var multiply = function(x,y) {
return (x * y);
}(2,2);
console.log(multiply);
//output: 4
3. Create your own jQuery function
This is an awesome way to declare functions that can be used just like your regular jQuery functions, on your DOM elements! Rememeber jQuery.fn is just an alias for jQuery.prototype (which just saves us time when coding such jQuery.fn.init.prototype = jQuery.fn = $.fn as such).
jQuery.fn.extend({
zigzag: function () {
var text = $(this).text();
var zigzagText = '';
var toggle = true; //lower/uppper toggle
$.each(text, function(i, nome) {
zigzagText += (toggle) ? nome.toUpperCase() : nome.toLowerCase();
toggle = (toggle) ? false : true;
});
return zigzagText;
}
});
console.log($('#tagline').zigzag());
//output: #1 jQuErY BlOg fOr yOuR DaIlY NeWs, PlUgInS, tUtS/TiPs & cOdE SnIpPeTs.
//chained example
console.log($('#tagline').zigzag().toLowerCase());
//output: #1 jquery blog for your daily news, plugins, tuts/tips & code snippets.
Don’t forget to return the element so that you can chain jQuery functions together.
4. Extend Existing jQuery Functions
(or which either extend existing jQuery functions with extra functionality or creating your own functions that can be called using the jQuery namespace (usually, we use the $ sign to represent the jQuery namespace). In this example the $.fn.each function has been modified with custom behaviour.
(function($){
// maintain a to the existing function
var oldEachFn = $.fn.each;
$.fn.each = function() {
// original behavior - use function.apply to preserve context
var ret = oldEachFn.apply(this, arguments);
// add custom behaviour
try {
// change background colour
$(this).css({'background-color':'orange'});
// add a message
var msg = '<span style="float:left;font-size:24px;font-weight:bold">Danger high voltage!</span>';
$(this).prepend(msg);
}
catch(e)
{
console.log(e);
}
// preserve return value (probably the jQuery object...)
return ret;
}
// run the $.fn.each function as normal
$('p').each(function(i,v)
{
console.log(i,v);
});
//output: all paragrahs on page now appear with orange background and high voltage!
})(jQuery);
5. Functions in custom namespaces
If your writing functions in a custom namespace you must declare them in this way. Extra functions can be added to the namespace you just need to add a comma after each one (except the last one!). If your unsure about namespacing see jQuery Function Namespacing in Plain English
JQUERY4U = {
multiply: function(x,y) {
return (x * y);
}
}
//function call
JQUERY4U.multiply(2,2);
Conclusion
Knowing when and how to declare different types of JavaScript/jQuery functions is definately something any good js developer should know inside out.
Read the full/original article at jQuery4u
5 Different Ways to Declare Functions in jQuery
Introduction
Choosing which way to declare a JavaScript function can be confusing for beginners as there are several different ways to declare functions using JavaScript/jQuery. I’ll try to explain the benefits of each one and how and why you might use them when writing your awesome jQuery code.
1. The basic JavaScript function
This is the simplest way to declare a function in JavaScript. Say for example, we want to write a simple function called multiply(x,y) which simply takes in two parameters x and y, does a simple x times y and returns the value. Here are a few ways you might go about doing exactly this.
function multiply(x,y) {
return (x * y);
}
console.log(multiply(2,2));
//output: 4
If you wanted a quick function to test something then maybe that’s the only occasion you would use this. It’s not good coding and doesn’t promote code reuse.
2. JavaScript functions for get/set
If you need a private utility for getting/setting/deleting model values then you can declare a function as a variable like this. This could be useful for assigning a variable upon declaration calculated by a function.
var multiply = function(x,y) {
return (x * y);
}
console.log(multiply(2,2));
//output: 4
//The same function but with a self execution to set the value of the variable:
var multiply = function(x,y) {
return (x * y);
}(2,2);
console.log(multiply);
//output: 4
3. Create your own jQuery function
This is an awesome way to declare functions that can be used just like your regular jQuery functions, on your DOM elements! Rememeber jQuery.fn is just an alias for jQuery.prototype (which just saves us time when coding such jQuery.fn.init.prototype = jQuery.fn = $.fn as such).
jQuery.fn.extend({
zigzag: function () {
var text = $(this).text();
var zigzagText = '';
var toggle = true; //lower/uppper toggle
$.each(text, function(i, nome) {
zigzagText += (toggle) ? nome.toUpperCase() : nome.toLowerCase();
toggle = (toggle) ? false : true;
});
return zigzagText;
}
});
console.log($('#tagline').zigzag());
//output: #1 jQuErY BlOg fOr yOuR DaIlY NeWs, PlUgInS, tUtS/TiPs & cOdE SnIpPeTs.
//chained example
console.log($('#tagline').zigzag().toLowerCase());
//output: #1 jquery blog for your daily news, plugins, tuts/tips & code snippets.
Don’t forget to return the element so that you can chain jQuery functions together.
4. Extend Existing jQuery Functions
(or which either extend existing jQuery functions with extra functionality or creating your own functions that can be called using the jQuery namespace (usually, we use the $ sign to represent the jQuery namespace). In this example the $.fn.each function has been modified with custom behaviour.
(function($){
// maintain a to the existing function
var oldEachFn = $.fn.each;
$.fn.each = function() {
// original behavior - use function.apply to preserve context
var ret = oldEachFn.apply(this, arguments);
// add custom behaviour
try {
// change background colour
$(this).css({'background-color':'orange'});
// add a message
var msg = '<span style="float:left;font-size:24px;font-weight:bold">Danger high voltage!</span>';
$(this).prepend(msg);
}
catch(e)
{
console.log(e);
}
// preserve return value (probably the jQuery object...)
return ret;
}
// run the $.fn.each function as normal
$('p').each(function(i,v)
{
console.log(i,v);
});
//output: all paragrahs on page now appear with orange background and high voltage!
})(jQuery);
5. Functions in custom namespaces
If your writing functions in a custom namespace you must declare them in this way. Extra functions can be added to the namespace you just need to add a comma after each one (except the last one!). If your unsure about namespacing see jQuery Function Namespacing in Plain English
JQUERY4U = {
multiply: function(x,y) {
return (x * y);
}
}
//function call
JQUERY4U.multiply(2,2);
Conclusion
Knowing when and how to declare different types of JavaScript/jQuery functions is definately something any good js developer should know inside out.
Read the full/original article at jQuery4u
Comments are closed.
Learn more about jQuery
I only just found out that the jQuery validation plugins has a validation rule called “remote” which can be used ...
jQuery function to check if horizontal scroll is present – hasHScrollBar() – (or vertical check below also, util function to ...
jQuery function to Set any DOM Element to Top View (bring to front) using CSS Z-Index property.
Is it possible to declare arrays in JavaScript object literal notation? Example 1 – this works with arrays Declaration: Storage ...
To get the a variables type using jQuery there is a jQuery function called .type() which returns “array”, “string”, “number”, ...
In this post you can find tutorials which explain step by step different API use cases with jQuery like Google ...
For valuable work on creation of sites you need a good comfortable editor necessarily. There are many requiring paid products ...
Today we are sharing with you a collection of awesome jQuery Camera Photo plugins. They offer a range of image ...
Back in the day, if you saw something that was animated on a website it was automatically assumed to be ...
In this post, we have compiled a list of 10 jQuery HTML5 Audio players available today, most allow native audio ...
jQuery Mobile is a powerful framework for making mobile web applications. But can we use it to convert existing desktop ...
I have jotted a quick post on a Basic JavaScript Regular Expression Example to give beginners out there a taste ...
In April 2012′s edition of Interestingly Random JavaScript, jQuery and Web Development we bring you some very cool stuff such ...
In this post we are sharing you a roundup of 10 really jQuery plugins as of today. Pretty cool plugins ...
As always expected from the jQuery community, the compilation we have for you today are some good and impressive recently ...
A collection of jQuery PNG/JPEG/GIF plugins that enables image animation, cartoon-like background, etc… let them help you design displays for ...
A collection of JavaScript/jQuery Zip/File/Compressor plugins that allows you to minify your JS code and compress your JS files ready ...
Quick jQuery code snippet on how to keep an element in view. For demo scroll down on any page on ...
Here is what I think is the best and most reliable way to load jQuery library and jQuery UI Libraries. ...
jQuery code snippet which outputs all attributes of element using the default .attr() function on any dom element(s). usage: output:
Use jQuery and the Google Maps API to create your own map application.
Super cool and easy way to get stylish tooltips with jQuery.
The right way to include the jQuery library in WordPress.
