HTML and CSS Reference
In-Depth Information
Keep in mind that although you may decide to avoid extending native objects,
others may not be so nice. Filtering for-in loops with
hasOwnProperty
—even
when you are not modifying
Object.prototype
and
Array.prototype
yourself—will keep your code running as expected, regardless of whether third-
party code such as libraries, ad, or analytics related code decide to do so.
7.1.5 Property Attributes
ECMA-262 defines four properties that may be set for any given property. It
is important to note that these attributes are set for properties by the inter-
preter, but JavaScript code you write has no way of setting these attributes.
The
ReadOnly
and
DontDelete
attributes cannot be inspected explicitly, but
we can deduce their values. ECMA-262 specifies the
Object.prototype.
propertyIsEnumerable
method, which could be used to get the value of
DontEnum
; however, it does not check the prototype chain and is not reliably
implemented across browsers.
7.1.5.1 ReadOnly
If a property has the
ReadOnly
attribute set, it is not possible to write to the pro-
perty. Attempting to do so will pass by silently, but the property attempted to update
will not change. Note that if any object on the prototype chain has a property with
the attribute set, writing to the property will fail.
ReadOnly
does not imply that the
value is constant and unchanging—the interpreter may change its value internally.
7.1.5.2 DontDelete
If a property has the
DontDelete
attribute set, it is not possible to delete it
using the
delete
operator. Much like writing to properties with the
ReadOnly
attribute, deleting properties with the
DontDelete
attribute will fail silently. The
expression will return false if the object either didn't have the given property, or if
the property existed and had a
DontDelete
attribute.
7.1.5.3 DontEnum
DontEnum
causes properties to not appear in
for-in
loops, as shown in
Listing 7.9. The
DontEnum
attribute is the most important property attribute
to understand because it is the one that is most likely to affect your code. In
Listing 7.7 we saw an example of how enumerable properties may trip up badly
written
for-in
loops. The
DontEnum
attribute is the internal mechanism that
decides whether or not a property is enumerable.