简体   繁体   中英

Is document.all an object or a function?

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

.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)

我会说document.all是一个返回对象/集合的函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM