I just want to write few things which I learned these days.
$.Callbacks - sort of a queue of functions, where all be executed at once on fire. Example:
function consoleLogtestOne() { console.log("test one") } function consoleLogtestTwo() { console.log("test two") } var myCallbacks = $.Callbacks(); myCallbacks.add(consoleLogtestOne); myCallbacks.add(consoleLogtestTwo); myCallbacks.fire();
With line of code:
myCallbacks.fire();
we are executing both functions at once.
Array.prototype.map(Function) - executes callback function for each member in array. Example:
/*global console*/ (function () { "use strict"; var myNumbers = [1, 4, 9], variableWhichWillDoNothing; variableWhichWillDoNothing = myNumbers.map(function (currentValue, index, array) { console.log("currentValue: " + currentValue); console.log("index: " + index); console.log("array: " + array); console.log("---"); return "nothing"; }); console.log("Just to do something with doubles (to satisfy jsLint): " + variableWhichWillDoNothing); }())
Result will be something like:
Function.prototype.apply() - provides arguments as array to function.
Check these two examples:
function applyLog() { console.log.apply(console, arguments); } function justLog() { console.log(arguments); }
If you execute justLog like this:
justLog("test1", "test2")
Result will something like this:
As you can see result is an array.
Now lets execute applyLog:
applyLog("test1", "test2")
Result is something like:
As you can see no array.
Using apply and map you can generate array of elements, for example:
Array.apply(null, Array(24)).map(function (_, i) {return ('0' + i)});
Result:
["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "010", "011", "012", "013", "014", "015", "016", "017", "018", "019", "020", "021", "022", "023"]
slice - select elememts
Check this example:
('stanko').slice(-1)
Result:
"o"
Another:
('stanko').slice(-2)
Result:
"ko"
One more:
('stanko').slice(2)
Result:
"anko"