JS: Object Overview
What is javascript object
A JavaScript Object is a collection of key-and-value pairs. e.g. {a:1, b:2}. Each key-and-value pair is called a Property.
Data object and special-purpose object
JavaScript objects can be categorized into 2 kinds:
- Data object
-
collection of key-and-value pairs. e.g.
{a:1, b:2}also known as Object Object.
- Special-purpose object
-
- Special purpose and may have internal data.
- For example, function, date, regex, etc, are specialized objects.
- They have special properties and behaviors.
- But they still have the feature of holding key-and-value pairs.
Prototype of an object (aka parent of an object)
- Each object MAY have ONE parent object. This object is call its prototype.
- The chain of parents is called Prototype Chain. The significance of Prototype Chain is how object's properties are looked up: When a property is accessed, JavaScript will go up the parent object or parent of parent etc to look for the property. (this is called inheritance.)
This is called Prototype-Based Object System.
Is extensible
Each object has a special true/false value attached called the [[isExtensible]] internal slot. It specifies whether new properties can be added to the object.
Source of object
It is useful to distinguish objects by where they came from. We have the following sources:
- Standard Object
-
Objects from the JavaScript language. e.g. arrays, functions, dates, regex, etc.
- Hosted Object
-
Objects from the hosting environment. e.g. in Browser, there's Browser's Window Object. In deno, there is
denoobject, with properties such as version etc. - User-defined Object