I'm looking over this code:
$(function(){
var $sidescroll = (function() {
init = function() {
//STUFF
};
return { init : init }; //What does this do?
})();
$sidescroll.init();
});
What does the return statement mean? I haven't seen curly braces in a return statement before, and am not even sure what 'init : init' does.
Curly braces mean two things in javascript:
You've probably seen the second -- also known in other languages as "dictionaries", key-value pairs, associative arrays, etc:
myDict = { a: "apple", b: "banana" };
When we say
return { a: "apple" };
it is the same as saying
myDict = { a: "apple" };
return myDict;
The "confusing" thing in this case is that (1) the key and the value are identical/have the same character representation, and (2) the value is not a normal string or variable but, a function. That is, accessing the key "init" of your object/dictionary will give you a function that you can call with ()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With