JS: Object Type
What is Object
JavaScript spec defines object as: “a collection of (key and 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 and 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 and 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
- JS: Object Tutorial
- JS: Object Overview
- JS: Object Type
- JS: Test is Object Type 💠
- JS: Determine Type of Object
- JS: Prototype and Inheritance
- JS: Prototype Chain
- JS: Object.prototype.isPrototypeOf
- JS: Get Set Prototype
- JS: Show Prototype Chain 💠
- JS: Create Object
- JS: Object Literal Expression
- JS: Create Object with Parent X
- JS: Prevent Adding Property
- JS: Deep Copy Array or Object 💠
- JS: Test Equality of Array and Object by Content 💠
- JS: Add Method to Prototype
- JS: Object Object
- JS: Object Constructor
- JS: Object.prototype