This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Example of Properties, keys & Values | |
| let user = { // An object | |
| name: 'akash', // 'name' is a key: 'akash' is a value | |
| age: 22 | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fullName = { | |
| first: 'Akash', | |
| last: 'Rajvanshi' | |
| }; | |
| console.log(fullName.first) // Akash | |
| // In this code, we create an object via an object literal, which starts with a curly brace and end with a curly brace {...}. | |
| // Inside it, we have propety: {Key = first, last} and {value = Akash, Rajvanshi} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const user = { | |
| firstName: 'Akash', | |
| lastName: 'Rajvanshi', | |
| fullname: function () { //method | |
| return `${this.firstName} ${this.lastName}` | |
| } | |
| }; | |
| console.log(user.fullname()) //Akash Rajvanshi | |
| console.log(user.firstName) //Akash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| console.log(user.fullname()) // call method fullname | |
| // Akash Rajvanshi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| user.firstName = 'Abha' // Set property 'firstName' | |
| console.log(user.firstName) // Abha |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| delete user.firstName | |
| console.log(user.firstName) //undefined | |
| console.log(Object.keys(user) // [ 'lastName', 'fullname' ] | |
| // But if we set a property to 'undefined', the property still exits and the object still contains its key. | |
| user.firstName = undefined | |
| console.log(Object.keys(user)) // [ 'lastName', 'fullname', 'firstName' ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Object.defineProperty(user, 'canBeDeleted', { | |
| value: 123, | |
| configurable: true | |
| }); | |
| Object.defineProperty(user, 'cannotBeDeleted', { | |
| value: 456, | |
| configurable: false | |
| }); | |
| console.log(delete user.cannotBeDeleted) // false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fullName = { | |
| first: 'Akash', | |
| last: 'Rajvanshi', | |
| get full() { | |
| return `${this.first} ${this.last}` | |
| } | |
| }; | |
| console.log(fullName.full) // Akash Rajvanshi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fullName = { | |
| first: 'Akash', | |
| last: 'Rajvanshi', | |
| set full(name) { | |
| const parts = name.split(' ') | |
| this.first = parts[0] | |
| this.last = parts[1] | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let fullName = { | |
| first: 'Akash', | |
| last: 'Rajvanshi' | |
| }; | |
| fullName = {...fullName, age: 22} | |
| console.log(fullName) // { first: 'Akash', last: 'Rajvanshi', age: 22 } | |
| // Extra Features | |
| // 1. Changing content of an object |