Certainly! Here are some short code examples for the methods available on the Object object in JavaScript:
- Object.create(proto, [properties]):
const person = {
name: 'John',
age: 30
};
const employee = Object.create(person, {
salary: {
value: 5000,
writable: true,
enumerable: true,
configurable: true
}
});
- 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 }
- Object.keys(obj):
const obj = { a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
console.log(keys); // Output: ["a", "b", "c"]
- Object.values(obj):
const obj = { a: 1, b: 2, c: 3 };
const values = Object.values(obj);
console.log(values); // Output: [1, 2, 3]
- 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]]
- 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 }
- 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 }
- Object.isFrozen(obj):
const obj = { a: 1, b: 2 };
Object.freeze(obj);
console.log(Object.isFrozen(obj)); // Output: true
- Object.isSealed(obj):
const obj = { a: 1, b: 2 };
Object.seal(obj);
console.log(Object.isSealed(obj)); // Output: true
- Object.hasOwnProperty(prop):
const obj = { a: 1, b: 2 };
console.log(obj.hasOwnProperty('a')); // Output: true
console.log(obj.hasOwnProperty('c')); // Output: false
- Object.is(obj1, obj2):
console.log(Object.is(5, 5)); // Output: true
console.log(Object.is(5, '5')); // Output: false
- 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 }
- 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
- 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
- Object.getPrototypeOf(obj):
const person = {
name: 'John',
age: 30
};
const obj = Object.create(person);
console.log(Object.getPrototypeOf(obj) === person); // Output: true
- Object.setPrototypeOf(obj, proto):
const person = {
name: 'John',
age: 30
};
const obj = {};
Object.setPrototypeOf(obj, person);
console.log(obj.name); // Output: John
- Object.getOwnPropertyNames(obj):
const obj = { a: 1, b: 2 };
const propertyNames = Object.getOwnPropertyNames(obj);
console.log(propertyNames); // Output: ["a", "b"]
- 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)]
- Object.toString():
const obj = {};
console.log(Object.toString(obj)); // Output: [object Object]
- 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)
- 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
- 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 }
- 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)]
- 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 }
- 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]]
- Object.getOwnPropertyNames(obj):
const obj = { a: 1, b: 2 };
const propertyNames = Object.getOwnPropertyNames(obj);
console.log(propertyNames); // Output: ["a", "b"]
- Object.isPrototypeOf(obj):
const person = {
name: 'John',
age: 30
};
const obj = Object.create(person);
console.log(person.isPrototypeOf(obj)); // Output: true
- Object.toString():
const obj = {};
console.log(Object.toString(obj)); // Output: [object Object]
- 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)
- Object.hasOwnProperty(prop):
const obj = { a: 1, b: 2 };
console.log(obj.hasOwnProperty('a')); // Output: true
console.log(obj.hasOwnProperty('c')); // Output: false