JavaScript Object Methods Explained!

JavaScript Object Methods Explained!

ยท

6 min read

1. assign()

It clones and copy the values of source object into target object and return the newly created object by merging them.

// 1. assign()
const fruit = {
    name: 'Water Melon',
    icon: '๐Ÿ‰',
    type: 'Fruit'
};

const beverages = {
    name: 'Milk',
    icon: '๐Ÿฅ›',
    type: 'Beverage'
};

const returnedBeverages = Object.assign(beverages, fruit);
console.log(returnedBeverages); // { name: 'Water Melon', icon: '๐Ÿ‰', type: 'Fruit' }
console.log(beverages); // { name: 'Water Melon', icon: '๐Ÿ‰', type: 'Fruit'}

2. entries()

Creates sub arrays of a given object containing a pair of key and value.

// 2. entries()
const fruit = {
  name: 'Water Melon',
  icon: '๐Ÿ‰',
  type: 'Fruit'
};

Object.entries(fruit); // [["name", "Water Melon"], ["icon", "๐Ÿ‰"], ["type", "Fruit"]]

3. freeze()

Prevents the existing property modifications of object & it don't allow you to add or remove any property in or from object.

// 3. freeze()

const fruit = {
    name: 'Water Melon',
    icon: '๐Ÿ‰',
    type: 'Fruit'
};
Object.freeze(fruit);
fruit.name = 'Mango'; // Throws an error
console.log(fruit.name); // Water Melon

4. seal()

It allows the modification of existing object properties but you cannot add new property into it.

// 4. seal()
const fruit = {
    name: 'Water Melon',
    icon: '๐Ÿ‰',
    type: 'Fruit'
};
Object.seal(fruit);
fruit.name = 'Mango'; // { name: 'Mango', icon: '๐Ÿ‰', type: 'Fruit'}
fruit.icon = '๐Ÿฅญ'; // { name: 'Mango', icon: '๐Ÿฅญ', type: 'Fruit'}
fruit.available = true; // { name: 'Mango', icon: '๐Ÿฅญ', type: 'Fruit'}

5. isSealed()

It check if object is sealed or not.

// 5. isSealed()
const fruit = {
    name: 'Water Melon',
    icon: '๐Ÿ‰',
    type: 'Fruit'
};
Object.isSealed(fruit); // false
Object.seal(fruit);
Object.isSealed(fruit); // true

6. fromEntries()

It takes 2 arrays. One array of keys. and other array of values, and map them as key and value of object.

// 6. fromEntries()
const keysValues = new Map([
    ['name', 'icon', 'type'],
    ['Water Melon', '๐Ÿ‰', 'Fruit']
]);
Object.fromEntries(keysValues); /** { name: 'Water Melon', icon: '๐Ÿ‰', type: 'Fruit'} */

7. defineProperties()

It defines the property into an object and add into it.

// 7. defineProperties
const fruit = {
    name: 'Water Melon',
    icon: '๐Ÿ‰'
};

Object.defineProperties(fruit, {
    type: {
        value: 42,
        writable: true
    }
});

console.log(fruit); // { name: 'Water Melon', icon: '๐Ÿ‰', type: 'Fruit'}

8. keys()

Returns the keys of an object which are enumerable.

// 8. keys()
const fruit = {
    name: 'Water Melon',
    icon: '๐Ÿ‰'
};

Object.defineProperties(fruit, {
    type: {
        value: 'Fruit',
        enumerable: false
    }
});

Object.keys(fruit); // ['name', 'icon']
Object.keys(fruit).length; // 2

9. getOwnPropertyNames()

This is similar to key() method but with small difference. It Returns the keys of an object without caring whether they are enumerable or not.

// 9. getOwnPropertyNames()
const fruit = {
    name: 'Water Melon',
    icon: '๐Ÿ‰',
    type: 'Fruit'
};
Object.getOwnPropertyNames(fruit); // ['name', 'icon', 'type']
Object.keys(fruit).length; // 3

10. is()

Checks whether the 2 values are same or different.

// 10. is()
const mango = {
    name: 'Mango',
    icon: '๐Ÿฅญ',
    type: 'Fruit'
};
const apple = {
    name: 'Apple',
    icon: '๐ŸŽ',
    type: 'Fruit'
};
Object.is(mango.type, apple.type); // true
Object.is(mango.name, apple.name); // false

11. values()

Returns an array of object values.

// 11. values()
const fruit = {
    name: 'Mango',
    icon: '๐Ÿฅญ',
    type: 'Fruit'
};
Object.values(fruit); // ['Mango', '๐Ÿฅญ', 'Fruit']

12. hasOwnProperty()

Checks whether the given property exists in object or not. It returns true if property exists otherwise false will be return.

// 12. hasOwnProperty()
const fruit = {
    name: 'Mango',
    icon: '๐Ÿฅญ',
    type: 'Fruit'
};
fruit.hasOwnProperty('name'); // true
fruit.hasOwnProperty('flavour'); // false

13. create()

It creates a new objects and make the prototype available from source object to target object.

// 13. create()
const fruit = {
    name: 'Mango',
      icon: '๐Ÿฅญ',
      type: 'Fruit',
      details() {
        console.log(`This is ${this.type} ${this.name} (${this.icon})`);
    }
};
const beverage = Object.create(fruit);
beverage.name = 'Milk';
beverage.name = '๐Ÿฅ›';
beverage.name = 'Beverage';
beverage.details(); // This is Beverage Milk (๐Ÿฅ›)

Thanks for reading!

Added on my twitter as well:

If you enjoyed this article kindly ๐Ÿ‘ it and make sure to follow me on twitter and hashnode. & share it.