As we know we can use Array prototype methods with function arguments just like below.
function abc() {
// As 'arguments' is a array like object which don't have Array prototype functions.
Array.prototype.push.call(arguments, 30);
console.log(arguments);
}
abc(10, 20); // Output is: [10, 20, 30]
So like wise I tried to use push on DOM element classList like below which gives me an error "Cannot set property length of [object Object] which has only a getter".
var bodyClassList = document.body.classList;
Array.prototype.push.call(bodyClassList, 'myClass');
Note: I have just tried to learn concepts so that's why I used push even it has builtin add() method.
So my question is:
On which objects we can use Array.prototype methods?
Thanks in advance.
Array.prototype
methods are quite generic methods that work on all kinds of objects. They're just getting and setting indexed properties and the.length
of the object they are called on. (And, with ES6, the.constructor
property ofArray
instances if they need to create a new instance). You can check the spec to see whatpush
does exactly.So basically, to answer your question…
…on all objects. That is, objects that don't throw exceptions when they are being operated on. Notice that
push
itself does not throw the error.Some examples:
The
arguments
object is very much likex
. TheclassList
is very much likey
.