Objects
Estimated reading time: 8 minutesobjectsis a collections of data . objects represent by{ ... }or array objects[{},{}]. objects isname valuepair 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/clonetarget ... source
let users = {
name: 'kamal',
age