up
This commit is contained in:
parent
ad499144a1
commit
6c9c2219ba
19 changed files with 91 additions and 54 deletions
|
@ -667,30 +667,26 @@ There's a standard algorithm for deep cloning that handles the case above and mo
|
|||
|
||||
## Summary
|
||||
|
||||
[todo rewrite]
|
||||
|
||||
Objects are associative arrays with several special features.
|
||||
|
||||
- Property keys are either strings or symbols.
|
||||
They store properties (key-value pairs), where:
|
||||
- Property keys must be strings or symbols (usually strings).
|
||||
- Values can be of any type.
|
||||
|
||||
Property access:
|
||||
To access a property, we can use:
|
||||
- The dot notation: `obj.property`.
|
||||
- Square brackets notation `obj["property"]`. Square brackets allow to take the key from a variable, like `obj[varWithKey]`.
|
||||
|
||||
- Read/write uses the dot notation: `obj.property` or square brackets `obj["property"]/obj[varWithKey]`.
|
||||
- The deletion is made via the `delete` operator.
|
||||
- Existance check is made by the comparison vs `undefined` or via the `in` operator.
|
||||
- Three forms of looping:
|
||||
- `for(key in obj)` for the keys.
|
||||
- `for(value of Object.values(obj))` for the values.
|
||||
- `for([key,value] of Object.entries(obj))` for both.
|
||||
Additional operators:
|
||||
- To delete a property: `delete obj.prop`.
|
||||
- To check if a property with the given key exists: `"key" in obj`.
|
||||
- To iterate over an object: `for(let key in obj)` loop.
|
||||
|
||||
- Ordering:
|
||||
- Integer properties in sorted order first, then strings in creation order, then symbols in creation order.
|
||||
- To keep the order for numeric properties, we can prepend them with `+` to make them look like non-numeric.
|
||||
Objects are assigned and copied by reference. In other words, a variable stores not the "object value", but a "reference" (address in memory) for the value. So copying such a variable or passing it as a function argument copies that reference, not the object. All operations via copied references (like adding/removed properties) are performed on the same single object.
|
||||
|
||||
- Objects are assigned and copied by reference.
|
||||
To make a "real copy" (a clone) we can use `Object.assign` or [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep).
|
||||
|
||||
What we've just seen is called a "plain object", or just `Object`.
|
||||
What we've studied in this chapter is called a "plain object", or just `Object`.
|
||||
|
||||
There are many other kinds of objects in Javascript:
|
||||
|
||||
|
@ -699,6 +695,6 @@ There are many other kinds of objects in Javascript:
|
|||
- `Error` to store the information about an error.
|
||||
- ...And so on.
|
||||
|
||||
Sometimes people say something like "Array type" or "Date type", but formally they are not types of their own, but belong to a single "object" data type. And they extend it in various ways.
|
||||
They have their special features that we'll study later. Sometimes people say something like "Array type" or "Date type", but formally they are not types of their own, but belong to a single "object" data type. And they extend it in various ways.
|
||||
|
||||
Objects in JavaScript are very powerful. Here we've just scratched the surface of the topic that is really huge. We'll be closely working with objects and learning more about them in further parts of the tutorial.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue