[This post is part of a
serieson JavaScript quirks.]
Some objects in JavaScript look like arrays, but aren’t. They are called array-like. This blog post looks at what exactly that means and how to best work with those objects.
Array-like objectsAn array-like object
Two examples of array-like objects is the result of the DOM method
document.getElementsByClassName()(many DOM methods return array-like objects) and the special variable
arguments [1]. You can determine the number of arguments via
arguments.length
And you can access a single argument, e.g. read the first argument:
arguments[0]
Array methods, however, have to be borrowed. You can do that, because most of those methods are
generic.
Generic methodsA generic method does not require
thisto be an array, it only requires
thisto have
lengthand indexed element access. Normally, you invoke a method
mon an array
arras follows.
arr.m(arg0, arg1, ...)
All functions have a method
callthat allows you to perform the above invocation differently:
Array.prototype.m.call(arr, arg0, arg1, ...)
The first argument of
callis the value for
thisthat
mreceives (in this case,
arr). Because we access
mdirectly and not via
arr, we can now hand any
thisto that method. For example,
arguments:
Array.prototype.m.call(arguments, arg0, arg1, ...)Examples
Let’s continue with a concrete example. The following function
printArgslogs all arguments that it receives.
function printArgs() { Array.prototype.forEach.call(arguments, function (arg, i) { console.log(i+'. '+arg); }); }
We have used method
forEachgenerically.
printArgsin use:
> printArgs() > printArgs('a') 0. a > printArgs('a', 'b') 0. a 1. b
You can even apply generic methods to ordinary objects:
> var obj = {}; > Array.prototype.push.call(obj, 'a'); 1 > obj { '0': 'a', length: 1 }
In the above case, property
lengthdid not exist and was automatically created, with the initial value zero.
Converting an array-like object to an arraySometimes the best way to work with an array-like object is to convert it to an array. That can also be done via a generic method:
Array.prototype.slice.call(arguments)
Compare: to create a copy of an array
arr, you make the method call
arr.slice()Reference
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4