
Objects in JavaScript are collections of Key/Value pairs. The values can consist of properties and methods. This post will go over important built-in object methods.
Object.create()
Used to create a new object and link it to the prototype of an existing object.
constperson={isHuman:false,printIntroduction:function(){console.log('My name is ${this.name}. Am I a human? ${this.isHuman}')}}constme=Object.create(person)me.name='Mursal'me.isHuman=trueme.printIntroduction()// Expected Output: "My name is Mursal. Am I a human? true"
Object.keys() and Object.values()
Object.keys(): Creates an array containing the keys of an object
Object.values(): Creates an array containing the values of an object
constnum={one:1,two:2,three:3}console.log(Object.Keys(num))// ['one', 'two', 'three']console.log(Object.Values(num))// [1, 2, 3]
Object.entries()
Used to create a nested array of the key/value pairs of an object
constnum={one:1,two:2,three:3}console.log(Object.entries(num))/* Expected Output [['one', 1], ['two', 2], ['three', 3]]*/
Object.assign()
It is used to copy values from one object to another.
constname={firstName:'Mursal',lastName:'Furqan'}constdetails={job:'Software Engineer',country:'Italy'}// Merge the objectconstcharacter=Object.assign(name,details)console.log(character)/* Expected Output { firstName: 'Mursal', lastName: 'Furqan', job: 'Software Engineer', country: 'Italy' }*/
Object.freeze()
Prevents modification to properties and values of an object, and prevents properties from being added or removed from an object
constuser={username:'mursalfurqan',password:'thisisalwayssecret'}// Freeze the objectconstnewUser=Object.freeze(user)newUser.password='*******************'newUser.active=trueconsole.log(newUser)/* Expected Output { username: 'something', password: 'thisisalwayssecret' }*/
Object.seal()
Prevents new properties from being added to an object, but allows the modification of existing properties.
constuser={username:'mursalfurqan',password:'thisisalwayssecret'}// Seal the objectconstnewUser=Object.seal(user)newUser.password='*******************'newUser.active=trueconsole.log(newUser)/* Expected Output { username: 'something', password: '******************' }*/
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse