Object’s In Details

Certainly! Here are some short code examples for the methods available on the Object object in JavaScript:

  1. Object.create(proto, [properties]):
const person = {
  name: 'John',
  age: 30
};

const employee = Object.create(person, {
  salary: {
    value: 5000,
    writable: true,
    enumerable: true,
    configurable: true
  }
});

  1. Object.assign(target, …sources):
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };

const merged = Object.assign(target, source);
console.log(merged); // Output: { a: 1, b: 3, c: 4 }

  1. Object.keys(obj):
const obj = { a: 1, b: 2, c: 3 };

const keys = Object.keys(obj);
console.log(keys); // Output: ["a", "b", "c"]

  1. Object.values(obj):
const obj = { a: 1, b: 2, c: 3 };

const values = Object.values(obj);
console.log(values); // Output: [1, 2, 3]

  1. Object.entries(obj):
const obj = { a: 1, b: 2, c: 3 };

const entries = Object.entries(obj);
console.log(entries); // Output: [["a", 1], ["b", 2], ["c", 3]]

  1. Object.freeze(obj):
const obj = { a: 1, b: 2 };

Object.freeze(obj);

obj.c = 3; // This assignment will have no effect
console.log(obj); // Output: { a: 1, b: 2 }

  1. Object.seal(obj):
const obj = { a: 1, b: 2 };

Object.seal(obj);

delete obj.a; // This deletion will have no effect
obj.c = 3; // This assignment will have no effect
console.log(obj); // Output: { a: 1, b: 2 }

  1. Object.isFrozen(obj):
const obj = { a: 1, b: 2 };

Object.freeze(obj);

console.log(Object.isFrozen(obj)); // Output: true

  1. Object.isSealed(obj):
const obj = { a: 1, b: 2 };

Object.seal(obj);

console.log(Object.isSealed(obj)); // Output: true

  1. Object.hasOwnProperty(prop):
const obj = { a: 1, b: 2 };

console.log(obj.hasOwnProperty('a')); // Output: true
console.log(obj.hasOwnProperty('c')); // Output: false

  1. Object.is(obj1, obj2):
console.log(Object.is(5, 5)); // Output: true
console.log(Object.is(5, '5')); // Output: false

  1. Object.getOwnPropertyDescriptor(obj, prop):
const obj = { a: 1 };

const descriptor = Object.getOwnPropertyDescriptor(obj, 'a');
console.log(descriptor); // Output: { value: 1, writable: true, enumerable: true, configurable: true }

  1. Object.defineProperty(obj, prop, descriptor):
const obj = {};

Object.defineProperty(obj, 'name', {
  value: 'John',
  writable: false,
  enumerable: true,
  configurable: true
});

console.log(obj.name); // Output: John
obj.name = 'Jane'; // This assignment will have no effect
console.log(obj.name); // Output: John

  1. Object.defineProperties(obj, props):
const obj = {};

Object.defineProperties(obj, {
  name: {
    value: 'John',
    writable: false,
    enumerable: true
  },
  age: {
    value: 30,
    writable: true,
    enumerable: true
  }
});

console.log(obj.name); // Output: John
console.log(obj.age); // Output: 30

  1. Object.getPrototypeOf(obj):
const person = {
  name: 'John',
  age: 30
};

const obj = Object.create(person);

console.log(Object.getPrototypeOf(obj) === person); // Output: true

  1. Object.setPrototypeOf(obj, proto):
const person = {
  name: 'John',
  age: 30
};

const obj = {};

Object.setPrototypeOf(obj, person);

console.log(obj.name); // Output: John

  1. Object.getOwnPropertyNames(obj):
const obj = { a: 1, b: 2 };

const propertyNames = Object.getOwnPropertyNames(obj);
console.log(propertyNames); // Output: ["a", "b"]

  1. Object.getOwnPropertySymbols(obj):
const symbol1 = Symbol('symbol1');
const symbol2 = Symbol('symbol2');
const obj = { [symbol1]: 'value 1', [symbol2]: 'value 2' };

const symbols = Object.getOwnPropertySymbols(obj);
console.log(symbols); // Output: [Symbol(symbol1), Symbol(symbol2)]

  1. Object.toString():
const obj = {};

console.log(Object.toString(obj)); // Output: [object Object]

  1. Object.toLocaleString():
const date = new Date();

console.log(Object.toLocaleString(date)); // Output: 6/10/2023, 1:23:45 AM (format may vary based on locale)

  1. Object.isExtensible(obj):
const obj = { a: 1, b: 2 };

console.log(Object.isExtensible(obj)); // Output: true

Object.preventExtensions(obj);

console.log(Object.isExtensible(obj)); // Output: false

  1. Object.preventExtensions(obj):
const obj = { a: 1, b: 2 };

Object.preventExtensions(obj);

obj.c = 3; // This assignment will have no effect
console.log(obj); // Output: { a: 1, b: 2 }

  1. Object.getOwnPropertySymbols(obj):
const symbol1 = Symbol('symbol1');
const symbol2 = Symbol('symbol2');
const obj = { [symbol1]: 'value 1', [symbol2]: 'value 2' };

const symbols = Object.getOwnPropertySymbols(obj);
console.log(symbols); // Output: [Symbol(symbol1), Symbol(symbol2)]

  1. Object.fromEntries(entries):
const entries = [['a', 1], ['b', 2], ['c', 3]];

const obj = Object.fromEntries(entries);
console.log(obj); // Output: { a: 1, b: 2, c: 3 }

  1. Object.entries(obj):
const obj = { a: 1, b: 2, c: 3 };

const entries = Object.entries(obj);
console.log(entries); // Output: [["a", 1], ["b", 2], ["c", 3]]

  1. Object.getOwnPropertyNames(obj):
const obj = { a: 1, b: 2 };

const propertyNames = Object.getOwnPropertyNames(obj);
console.log(propertyNames); // Output: ["a", "b"]

  1. Object.isPrototypeOf(obj):
const person = {
  name: 'John',
  age: 30
};

const obj = Object.create(person);

console.log(person.isPrototypeOf(obj)); // Output: true

  1. Object.toString():
const obj = {};

console.log(Object.toString(obj)); // Output: [object Object]

  1. Object.toLocaleString():
const date = new Date();

console.log(Object.toLocaleString(date)); // Output: 6/10/2023, 1:23:45 AM (format may vary based on locale)

  1. Object.hasOwnProperty(prop):
const obj = { a: 1, b: 2 };

console.log(obj.hasOwnProperty('a')); // Output: true
console.log(obj.hasOwnProperty('c')); // Output: false