Convert arguments to Array
The arguments
object thats automatically available within functions can be a source of confusion for some people; it's kind of an array but it's kinda not. JavaScript is awesome in that you can pass any number of arguments
to a function, and oftentimes developers need to iterate over every argument provided. The arguments
object doesn't have a forEach method, but using a quick JavaScript technique, you can convert arguments
to an array:
function myFn(/* any number of arguments */) {
var args = Array.prototype.slice.call(arguments);
// or [].slice.call(arguments)
args.forEach(function(arg) {
// do something with args here
});
}
Much like converting a NodeList to an array, Array's slice
method takes the arguments
object and converts it to a true array, allowing for forEach, map, and traditional array iteration. Keep that trick up your sleeve for future development.
With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities. I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...
Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...
GitHub seems to change a lot but not really change at all, if that makes any sense; the updates come often but are always fairly small. I spotted one of the most recent updates on the pull request page. Links to long branch...
In my oh-so-humble opinion, Facebook's Modal box is the best modal box around. It's lightweight, subtle, and very stylish. I've taken Facebook's imagery and CSS and combined it with MooTools' awesome functionality to duplicate the effect.
The Imagery
Facebook uses a funky sprite for their modal...
If you’re using Firefox, or in the future when other browsers support ES6, you can do it more elegantly:
In case you are using Mootools,
Array.from
does the job, isn’t it ?Why not it:
You can do that but be aware if you do something like:
Cool trick, but why even
typeof arguments
returnsobject
?We have a new method,
Array.from(arguments)