Objects
Estimated reading time: 8 minutesobjects
is a collections of data . objects represent by{ ... }
or array objects[{},{}]
. objects isname value
pair base.
create objects
- constructor
let users = new Object()
- literal object with square bracket
{..}
let users = {
......
}
let users = {
name: 'kamal',
age: 21,
phone: 8123
}
console.log(users.name);
// kamal
let users = {
name: 'kamal',
age: 21,
phone: {
home: 88012,
office: 121
}
}
console.log(users.phone.office);
// 121
let users = [
{name: 'kamal', age: 21},
{name: 'jamal', age: 23},
{name: 'maruf', age: 6}
]
users.forEach((user) => {
console.log(user.name);
})
// kamal , jamal , maruf
- es6 loop
let users = [
{name: 'kamal', age: 21 ,post: 'marketer'},
{name: 'jamal', age: 23, post: 'civil'},
{name: 'maruf', age: 6, post: 'ceo'}
]
for (let {name,post} of users) {
console.log(`${name} is posted as ${post}`);
}
// kamal isposted as marketer
// jamal is posted as civil
// maruf is posted as ceo
multiword property
set
let users = {
name: 'kamal',
age: 21,
phone: 017
}
users['salary rate'] = 2000;
console.log(users);
// {name: "kamal", age: 21, phone: 15, salary rate: 2000}
get
let users = {
name: 'kamal',
age: 21,
phone: 017
}
users['salary rate'] = 2000;
console.log(users['salary rate']);
// 2000
delete
let users = {
name: 'kamal',
age: 21,
phone: 017
}
users['salary rate'] = 2000;
delete users['salary rate'];
console.log(users['salary rate']);
// undefined
// deleted
method lists
methods | methods | methods |
---|---|---|
Object.assign() | Object.create() | Object.defineProperty() |
Object.defineProperties() | Object.entries() | Object.freeze() |
Object.getOwnPropertyDescriptor() | Object.getOwnPropertyDescriptors() | . |
Object.getOwnPropertyNames() | Object.getOwnPropertySymbols() | Object.values() |
Object.getPrototypeOf() | Object.is() | Object.isExtensible() |
Object.isFrozen() | Object.isSealed() | Object.keys() |
Object.preventExtensions() | Object.seal() | Object.setPrototypeOf() |
assign
copy
/clone
target ... source
let users = {
name: 'kamal',
age: 21,
phone: '0172'
}
let admin = Object.assign({},users);
console.log(admin);
// {name: "kamal", age: 21, phone: "0172"}
entries
- return
key
andvalue
array .
let users = {
name: 'kamal',
age: 21,
phone: '0172'
}
let result = Object.entries(users);
console.log(result);
result.forEach (([key,value]) => {
console.log(`${key} is ${value}`);
})
// 0: ["name", "kamal"]
//1:["age", 21]
// 2: ["phone", "0172"]
// name is kamal
// age is 21
// phone is 0172
freeze
- freeze method make objects constant , can’t add or delete objects properties.
~ output same
let users = {
name: 'kamal',
age: 21,
phone: '0172'
}
let result = Object.freeze(users);
users['position'] = 'coder';
delete users['name'];
console.log(users);
// {name: "kamal", age: 21, phone: "0172"}
~ strict mode
'use strict';
let users = {
name: 'kamal',
age: 21,
phone: '0172'
}
let result = Object.freeze(users);
users['position'] = 'coder';
delete users['name'];
console.log(users);
// Cannot add property position, object is not extensible
getOwnPropertyDescriptor
let users = {
name: 'kamal',
age: 21,
phone: `567`
}
let result = Object.getOwnPropertyDescriptor(users,name);
console.log(result.value)
// kamal
getOwnPropertyDescriptors
let users = {
name: 'kamal',
age: 21,
phone: `567`
}
let result = Object.getOwnPropertyDescriptors(users);
console.log(result.name.value);
console.log(result.age.value);
// kamal
// 21
getOwnPropertyNames
let users = {
name: 'kamal',
age: 21,
phone: `567`
}
let result = Object.getOwnPropertyNames(users);
console.log(result);
// ["name", "age", "phone"]
getPrototypeOf
let users = {
name: 'kamal',
age: 21,
phone: `567`
}
let mk = Object.create(users);
console.log(Object.getPrototypeOf(mk) === users);
// true
is
let result = Object.is('kamal', 'kamal')
console.log(result)
// true
keys
let users = {
name: 'kamal',
age: 21,
phone: `567`
}
let result = Object.keys(users);
console.log(result);
// ["name", "age", "phone"]
values
let users = {
name: 'kamal',
age: 21,
phone: `567`
}
let result = Object.values(users);
console.log(result);
// ["kamal", 21, "567"]