May 2, 201115 yr From what im studying, it seems a completely new language to javascript, and its supposedly written to make the js development an ease. The book i am reading is My link
May 2, 201115 yr jQuery is a Javascript library. It uses javascript but is designed to make using javascript much easier to use. There are tons of tutorials about it here Edited May 2, 201115 yr by itodd
May 2, 201115 yr Author ok thanks.Im trying to understand what are anonymous functions in javascript! Please help!
May 2, 201115 yr In JavaScript, functions may have names, or they may not. Functions without names are known as "anonymous". There's no performance benefit one way or another. Named functions make it a lot easier to reference the function while inside it (such as a recursive function) but require a little more typing. Here's a named function: function deleteOne(start) { if (start < 0 || typeof(start) !== "number") { throw "Well this isn't going to work..."; } else if (start === 0) { alert("We've hit 0!"); } else { // In this function, we can use 'deleteOne' to reference itself. deleteOne(start - 1); } } It's also possible to to put store named functions in a variable, as a property or pass them to a function: // This named function is stored in a variable. // They can have different names, or the same. var myFunc = function myFunc(foo) { // Here "myFunc" refers to this function. }; // Same as above, but with a different name. var myFunc = function myNamedFunc(foo) { // Here "myNamedFunc" refers to this function. // "myFunc" is the variable. It refers to the same function as "myNamedFunc" // but is in a higher scope, so is slightly slower to resolve than "myNamedFunc" }; // Here is an object literal var myObj = { // This has the same rules as the named function above. foo: function bar() { } }; // Here we pass a named function to another function. myFunc(function fooBar() { alert('FOOBAR!'); }); Anonymous functions simply don't have the name. It's harder to refer to these functions within themselves, but if you're not recursing, this isn't a problem. // Here's a function stored in a variable. var myFunction = function () { alert('Fear us for we are Anonymous!'); }; // Here is an object literal var myObj = { foo: function () { } }; // Here the function is being passed to another function myFunction(function () { alert('Totally anonymous'); }); Anonymous functions have one useful bonus. You often see JavaScript libraries wrapped in this pattern: (function () { // This is a self-executing anonymous function. It has a few advantages a magic powers, // The main thing it does it contain the scope so the global scope isn't flooded. In here // all variables declared in here don't exist outside this function. // Frequently short-cuts to window, document and undefined are passed as arguments. }()); Does that help answer your question? Edited May 2, 201115 yr by Skateside
May 3, 201115 yr Author In JavaScript, functions may have names, or they may not. Functions without names are known as "anonymous". There's no performance benefit one way or another. Named functions make it a lot easier to reference the function while inside it (such as a recursive function) but require a little more typing. Here's a named function: function deleteOne(start) { if (start < 0 || typeof(start) !== "number") { throw "Well this isn't going to work..."; } else if (start === 0) { alert("We've hit 0!"); } else { // In this function, we can use 'deleteOne' to reference itself. deleteOne(start - 1); } } It's also possible to to put store named functions in a variable, as a property or pass them to a function: // This named function is stored in a variable. // They can have different names, or the same. var myFunc = function myFunc(foo) { // Here "myFunc" refers to this function. }; // Same as above, but with a different name. var myFunc = function myNamedFunc(foo) { // Here "myNamedFunc" refers to this function. // "myFunc" is the variable. It refers to the same function as "myNamedFunc" // but is in a higher scope, so is slightly slower to resolve than "myNamedFunc" }; // Here is an object literal var myObj = { // This has the same rules as the named function above. foo: function bar() { } }; // Here we pass a named function to another function. myFunc(function fooBar() { alert('FOOBAR!'); }); Anonymous functions simply don't have the name. It's harder to refer to these functions within themselves, but if you're not recursing, this isn't a problem. // Here's a function stored in a variable. var myFunction = function () { alert('Fear us for we are Anonymous!'); }; // Here is an object literal var myObj = { foo: function () { } }; // Here the function is being passed to another function myFunction(function () { alert('Totally anonymous'); }); Anonymous functions have one useful bonus. You often see JavaScript libraries wrapped in this pattern: (function () { // This is a self-executing anonymous function. It has a few advantages a magic powers, // The main thing it does it contain the scope so the global scope isn't flooded. In here // all variables declared in here don't exist outside this function. // Frequently short-cuts to window, document and undefined are passed as arguments. }()); Does that help answer your question? Yes thanks to a certain.Its when theyre being used in jQUERY is where im referring to, so your explanation at the end is great.
Create an account or sign in to comment