Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Let's explore Objects Methods in JavaScript
Mursal Furqan Kumbhar
Mursal Furqan Kumbhar

Posted on

     

Let's explore Objects Methods in JavaScript

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"
Enter fullscreen modeExit fullscreen mode

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]
Enter fullscreen modeExit fullscreen mode

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]]*/
Enter fullscreen modeExit fullscreen mode

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'     }*/
Enter fullscreen modeExit fullscreen mode

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' }*/
Enter fullscreen modeExit fullscreen mode

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: '******************' }*/
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Trying to make the 🌍 a better place using 🧑‍💻 AWS Community Builder | Love Coding | ☕
  • Location
    Rome, Itlay
  • Education
    M.Sc in Computer Science (Software Engineering)
  • Pronouns
    He/Him/His
  • Work
    Graduate Student at Sapienza University of Rome
  • Joined

More fromMursal Furqan Kumbhar

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp