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.
![How I Stopped WordPress Comment Spam]()
I love almost every part of being a tech blogger: learning, preaching, bantering, researching. The one part about blogging that I absolutely loathe: dealing with SPAM comments. For the past two years, my blog has registered 8,000+ SPAM comments per day. PER DAY. Bloating my database...
![Send Text Messages with PHP]()
Kids these days, I tell ya. All they care about is the technology. The video games. The bottled water. Oh, and the texting, always the texting. Back in my day, all we had was...OK, I had all of these things too. But I still don't get...
![Create Your Own Dijit CSS Theme with LESS CSS]()
The Dojo Toolkit seems to just get better and better. One of the new additions in Dojo 1.6 was the use of LESS CSS to create Dijit themes. The move to using LESS is a brilliant one because it makes creating your own Dijit theme...
![Use Custom Missing Image Graphics Using MooTools]()
Missing images on your website can make you or your business look completely amateur.
Unfortunately sometimes an image gets deleted or corrupted without your knowledge.
You'd agree with me that IE's default "red x" icon looks awful, so why not use
your own missing image graphic?
The MooTools JavaScript
Note that...
If you’re using Firefox, or in the future when other browsers support ES6, you can do it more elegantly:
function myFn(...args) { /* code */ }In case you are using Mootools,
Array.fromdoes the job, isn’t it ?Why not it:
You can do that but be aware if you do something like:
function x(){ return Array.apply(null, arguments); }; x(10); //=> it will return empty array with a length of 10 instead of [10]Cool trick, but why even
typeof argumentsreturnsobject?We have a new method,
Array.from(arguments)