I’ve opened jQuery 1.7.1 library and wanted to study the code, but I’ve found a that functions are declared in strange way (for me). For example:
show: function() {
//some code here
},
I learned to define function on this way:
function show() {
//some code here
}
Can someone explain me why show function is not written on second way (like in most tutorials on internet)?
This is because it is within an object.
Object Literalshave their properties defined in this way:Where value can be nearly anything such as a number, string, function, or even another object. In JavaScript you can also declare
anonymous functionsand assign them to a variable. In fact, the following declarations have the same effect:So, combining those two concepts if I have an object and I want one of its properties to be a function I would do it like so:
You would then get the output:
Initializing!. Make sure to check out the great documentation and tutorials on the Mozilla Developer Network, including their JavaScript Tutorial SeriesHope this helps!