JavaScript: Object Type
What is Object
JavaScript spec defines object as: “a collection of key/value pairs”. (each pair is called a property of the object. [see Property Overview] )
Object is any value that is NOT any of { • undefined
• null
• true
• false
• string • number (including NaN
, Infinity
) • symbol }. [see Value Types]
For example, the following are all objects: array, function, date, regex.
Test If a Value is Object Type
Data Object (aka Object Object)
The data object, e.g.
{a:1, b:2}
, is the best example of a collection of key/value pairs.
We often call this
data object
or just
object.
JavaScript spec calls it
object object.
Special Purpose Objects
All objects other than the “data object” have special purposes and or hold internal data for that purpose.
For example,
- A function object
function f (x) { return x+1; }
is a function. - A RegExp Object
/a+/
is for matching string. - A Date Object represents date.
They are all objects, but they are usually called by other names, such as function, array, regex, etc. For a list of all builtin objects, see JavaScript Object Reference
You Can Add Properties to Any Object
You can add properties to function, date, regexp, etc., even though they are not usually used as key/value pairs.
// example of adding properties to different objects // array const aa = [3, 4]; aa["kk"] = 2; console.log(aa.hasOwnProperty("kk")); // function const f1 = function () { return 3; }; f1["kk"] = 2; console.log(f1.hasOwnProperty("kk")); // arrow function const f2 = (() => 3); f2["kk"] = 2; console.log(f2.hasOwnProperty("kk")); // date const dd = new Date(); dd["kk"] = 2; console.log(dd.hasOwnProperty("kk")); // RegExp const rx = /\d+/; rx["kk"] = 2; console.log(rx.hasOwnProperty("kk"));
JavaScript Object and Inheritance
- Object Basics
- Object Overview
- Object Type
- Test If a Value is Object Type 🚀
- Find Object's Type
- Prototype and Inheritance
- Prototype Chain
- Is in Prototype Chain?
- Get/Set Parent
- Show Parent Chain 🚀
- Create Object
- Object Literal Expr
- Create Object + Parent
- Prevent Adding Property
- Clone Object 🚀
- Test Object Equality 🚀
- Add Method to Prototype