Last active
March 12, 2017 03:03
-
-
Save dgcoffman/8c736c421a9bb43d0734c36e6a6f23e4 to your computer and use it in GitHub Desktop.
Description of how JavaScript objects work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Objects have four states: | |
properties can be added | deleted | modified* | do it with | check with | |
Default √ | √ | √ | n/a | n/a | |
preventExtensions X | √ | √ | preventExtensions | isExtensible | |
seal X | X | √ | seal | isSealed | |
freeze X | X | X | freeze | isFrozen | |
* by changing any of the four attributes below | |
You can make an object in three different ways: | |
new Object(), Object.create(), or using the literal notation (initializer notation) | |
Properties come in two flavors: | |
I. Data properties | |
II. Accessor properties | |
Data properties have four attributes: | |
1. value | |
The property's value. | |
2. writable | |
true or false. Whether the property's value can be written to. (For example, o["p"] = 1) | |
3. enumerable | |
true or false. Whether the property shows in some loop constructs, such as for (var x in o) {…} and Object.keys(o). | |
4. configurable | |
true or false. Whether the property can be deleted (For example, delete o["p"]) and whether its attributes can be changed. | |
Accessor properties have the same writeable and configurable attributes as data properties, but in addition (respectively): | |
5. get | |
6. set | |
Properties an be created with: | |
Object.defineProperty(obj, 'propName', { value, writable, enumerable, configurable }); | |
Defaults given an empty object are: | |
{ value: undefined, writable: false, enumerable: false, configurable: false } | |
However, creating a new property by assigning to it uses defaults: | |
{ value: "your value", writable: true, enumerable: true, configurable: true } | |
Property attributes can be accessed with: | |
Object.getOwnPropertyDescriptor(obj) or | |
Object.getOwnPropertyDescriptor(obj, "propName") or | |
Reflect.getOwnPropertyDescriptor(obj, "propName") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment