JSFiddle - React, Tailwind, and code Playground
by infralabs
HTML
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
JavaScript
//parsing function's arguments with forEach & call methods
//This snippet demonstrates how you can parse function's arguments into array with forEach with call methods.
function forEachCall() {
[1,2,3].forEach(function (element,index) {
console.log("["+index+"] = "+element);
}
);
//console.log(arguments);
//[1,2,3].forEach.call(arguments, function (element,index) {
[].forEach.call(arguments, function (element,index) {
console.log("["+index+"] = "+element);
}
);
}
forEachCall("a","b","c");
/*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments
//
arguments
- parametry pod postacią tablicy
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
//
arr.forEach(callback[, thisArg]);
- pozwala na zastosowanie funkcji, czyniącej określoną rzecz z przekazywanym po kolei elementem tablicy
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
//
fun.call(thisArg[, arg1[, arg2[, ...]]])
- przekazuje uszeregowane parametry do funkcji/metody
- jako 'thisArg' przekazywana jest tablica 'arguments', która podmienia zawartość [] lub [1,2,3] i następnie po tym jest wykonywana metoda forEach, która to metoda wykonuje wskazany callback
http://stackoverflow.com/questions/2125714/explanation-of-slice-call-in-javascript
[].slice returns a function object. A function object has a function call() which calls the function assigning the first parameter of the call() to this; in other words, making the function think that it's being called from the parameter (the NodeList returned by document.querySelectorAll('a')) rather than from an array.
*/