JS: Object Overview
Object syntax basics
What is 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 (parent of an object)
- Each object MAY have ONE parent object. Just one, or none. This object is call its prototype.
- The chain of parents is called Prototype Chain.
- When a property is accessed, JavaScript will go up the parent object or parent of parent etc to look for the property, until found, or no more parent. (this is called inheritance.)
Is extensible
Each object may be made non-extensible, meaning, you cannot add properties to it.
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