Home > Software engineering >  Is document.all an object or a function?
Is document.all an object or a function?

Time:06-12

The title is the question. (Do not mention functions being objects that are callable; it is not the conventional categorization.)

CodePudding user response:

.all is a property of the document (or prototype), and that property is a getter that returns a function. In other words, referencing the .all property invokes the getter (which is one function), and then the getter gives you another function. This behavior is specified here (but is way obsolete, and not something you should ever see nowadays).

For example, in Chrome, you can see that the property is a getter in the following:

console.log(
  Object.getOwnPropertyDescriptor(Document.prototype, 'all')
);

and that the value returned is callable by calling it:

document.all();

The value returned by the getter is an HTMLAllCollection.

It doesn't inherit from Function.prototype, so it's not a normal function - it's an exotic object - but it does have a [[Call]] internal method and so is callable and can be called a function. (and functions are also objects, of course, as you know)

CodePudding user response:

I would say document.all is an function that returns an object/collection.

  • Related