NodeList object length property readonly, how to check?
The length property descriptor property shows configurable: true, writeable: true, and enymerable: true, but it behaves like read only.
I knew that readonly function can only be implemented by PDO (Property Descriptor Object).
Can someone tell me? How is it read only?
var nodeList = document.getElementsByName('demo');
nodeList.length; //3
nodeList.length = 6;
nodeList.length; //3
Object.getOwnPropertyDescriptor(nodeList,'length')
Object
configurable: true
enumerable: true
value: 3
writable: true
__proto__: Object
source to share
length
is read-only, according to http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-536297177
Indeed, the mapping is wrong, but that's because it's a host object, so it shouldn't behave like the native object we're used to. This deserves a bug report as the display should match the behavior as closely as possible, especially in such obvious cases.
source to share
The DOM spec defines length
as read-only:
interface NodeList {
getter Node? item(unsigned long index);
readonly attribute unsigned long length;
};
It might seem like getOwnPropertyDescriptor
you shouldn't say that it's writable. However, NodeList
instances of host objects :
an object provided by the host environment to terminate the ECMAScript environment
Therefore, they can have special behavior. In fact, this mode is executed by ECMAScript :
If a property is described as a data property and it can return different values over time, then either one or both of the [[Writable]] and [[Configurable]] attributes must be true, even if the change in value is displayed through other internal methods.
Since it getElementsByName
returns a live collection, it length
can change, so [[Writable]] or [Configurable]] must be true
. Your implementation chooses both.
source to share