Except the primitives
Anything that is not a primitive value is an Object. That includes arrays, functions, and objects themselves.
An object is a
collection of properties
A
property is a
named container for a
value
w/ some additional attributes
The name of a property is called a key ;
thus, an object can be considered as
a collection of key-value pairs .
There are similar concepts in other programming languages,
e.g.,
Map, Dictionary, Associative Array, Symbol Table, Hash Table
, ...
all objects in JavaScript are maps (dictionaries) from strings to values.
A (key, value) entry in an object is called a property . The key of a property is always a text string (in ES5).
Properties (or named data properties)
Accessors (or named accessor properties)
Internal properties
Exist only in the ECMAScript language specification.
Literal Notation
const obj = { };
// Creating an object using literal notation
const obj = {
firstName: 'Alex',
'lastName': 'Martin', // Using string quotes
dateOfBirth: '18th October',
friends: [ 'Bob', 'Christine' ]
};
Object Constructor
Object.create
const obj = new Object();
obj.name = 'arfat'
const newObj = Object.create(Object.prototype);
newObj.name = 'arfat'
Dot Notation
$ jane.name = 'JANE'
$ jane['desc.func'] = function () {
return 'NEW FUNCTION'
}
Bracket Notation
Dot Notation
var jane = {
name: 'Jane',
'desc.func': function () {
return 'Person named ' + this.name;
},
};
$ jane.name
// 'jane'
$ jane['desc.func']
// [Function]
Bracket Notation
Note: Accessing non-existing properties give `undefined`
Deletion is performed using thedelete operator. Again, we can use both the notations.
delete obj.firstName; // => true
delete obj['firstName']; // => true
Note: The return value of delete operator is true if the property was successfully deleted. Else, it will be false.
Function as key values
const obj1 = {
fName: 'ALEX',
lName: 'MARTIN',
printFullName() {
return this.fName + ' ' + this.lName;
},
};
obj1.printFullName();
//=> 'ALEX MARTIN'
Sometimes we don’t care what value a property has. We only want to know whether the given object has the given property or not.
There are 2 ways to know that —
in operator
'firstName' in obj; // => true
'middleName' in obj; // => false
'isPrototypeOf' in obj; // => true
hasOwnProperty
obj.hasOwnProperty('firstName');
// => true
obj.hasOwnProperty('middleName');
// => false
obj.hasOwnProperty('isPrototypeOf');
// => false
Popular Methods
for-in loop
for-in loop is just like a normal for loop except that it iterates on an object. You get the properties* one by one.
const obj = {
firstName: 'Alex',
'lastName': 'Martin', // Using string quotes
dateOfBirth: '18th October',
friends: [ 'Bob', 'Christine' ]
};
for (const key in obj) {
const value = obj[key]; // Read the value
console.log(key, value);
}
Note: It is not recommended to use this method
Object methods
Object.keys()
const allProperties = Object.keys(obj);
// => [ 'firstName', 'lastName', 'dateOfBirth', 'friends' ]
for (const property of allProperties) {
const value = obj[property];
console.log(property, value);
}
Object.keys() is similar to for-in loop except that it only returns the object’s own keys in an array.
Object.values()
Object.values() has similar restrictions as Object.keys(), but it returns an array of values instead of the keys.
const allValues = Object.values(obj);
// => [ 'Alex', 'Martin', '18th October', [ 'Bob', 'Christine' ] ]
Object.entries()
const allEntries = Object.entries(obj);
// => Output[ [ 'firstName', 'Alex' ],
// [ 'lastName', 'Martin' ],
// [ 'dateOfBirth', '18th October' ],
// [ 'friends', [ 'Bob', 'Christine' ] ] ]
Reflect.ownKeys()