What is the difference between following forms of function definition:
$.myFunction = function() {
//function body
};
var myFunction = function() {
//function body
};
$(myFunction = function() {
//function body
});
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This creates a function as a property of the
$object. If you’re using jQuery then$is the globaljQueryobject. You would call this function with$.myFunction()This creates a function assigned to a variable called
myFunction. If you do this inside another function, thenmyFunctionwill only be available in that scope.This one is doing two things, let’s break it down into two steps to make it more clear:
myFunction. The variable is global because you didn’t use thevarkeyword. In general, you probably don’t want to use a global variable, and certainly not when its definition isn’t very obvious.The second step is (assuming you’re using jQuery) passing
myFunctionto jQuery to be executed when the DOM has finished loading. See the jQuery documentation for more information.It’s more common to pass a function to jQuery without assigning it to any variable, i.e.