tags:

views:

124

answers:

2

Hi,

I'm looking at this .js file and the jquery looks like:

$.fn.redirect
$.redirect
$.request

or

(function($){$.log=function(message){if(window.console){if(window.console.debug)
window.console.debug(message);else if(window.console.log)
window.console.log(message);}
else
alert(message);};

Are these built-in jQuery methods or is it custom? Just trying to understand this notation.

Is (function($){}; a way to start of your .js file?

A: 

(function(){ /* your code here */ })(); is a self executing function and a closure. It is used to encapsulate vars and functions so they do not conflict with unknown global vars. It can also be used in loops to make sure something executes right away with the correct value of i. in short it is a very valuable tool in JS development.

$.fn.someFunctionName = function() { /* your code here * /} is a way to extend jQuery. have a look at the extensions documentation for jQuery.

Darko Z
+1  A: 
(function($){
    // code
})(jQuery);

is an idiom commonly used so that the $ alias can be used in a localized manner without affecting the rest of the page where other libraries such as Prototype (with its own $) could have been possibly used.

jQuery in Action explains the working of this idiom: "By passing jQuery to a function that defines the parameter as $, $ is guaranteed to reference jQuery within the body of the function."

Please format your code in the second block properly.

Vijay Dev