Home  >  Article  >  Web Front-end  >  JavaScript Object Methods Example

JavaScript Object Methods Example

Susan Sarandon
Susan SarandonOriginal
2024-11-05 13:26:02181browse

JavaScript Object Methods Example

JavaScript Object Methods Example.

  • Object.keys(obj): Returns an array of an object's own enumerable property names (keys).
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.keys(obj));
// Output: ['a', 'b', 'c']
  • Object.values(obj): Returns an array of the object's own enumerable property values.
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.values(obj));
// Output: [1, 2, 3]
  • Object.entries(obj): Returns an array of the object's own enumerable string-keyed property [key, value] pairs.
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.entries(obj));
// Output: [['a', 1], ['b', 2], ['c', 3]]
  • Object.isSealed(obj): Returns true if the object is sealed, otherwise false.
const obj = Object.seal({ a: 1 });
console.log(Object.isSealed(obj));
// Output: true
  • Object.assign(target, source): Copies the values of all enumerable properties from one or more source objects to a target object. It returns the target object.
const target = { a: 1 };
const source = { b: 2, c: 3 };
const result = Object.assign(target, source);
console.log(result);
// Output: { a: 1, b: 2, c: 3 }
  • Object.freeze(obj): Freezes an object, preventing new properties from being added or existing properties from being removed or reconfigured.
const obj = { name: 'Khabib' };
Object.freeze(obj);
obj.name = 'Bob'; // This won't change the value
console.log(obj.name); // Output: 'Khabib'
  • Object.seal(obj): Seals an object, preventing new properties from being added, but allowing existing properties to be modified.
const obj = { name: 'Alice' };
Object.seal(obj);
obj.name = 'Bob'; // This will update the value
obj.age = 25; // This won't add a new property
console.log(obj); // Output: { name: 'Bob' }
  • Object.create(proto): Creates a new object with the specified prototype object and properties.
const person = {greet() {console.log('Hello!');}};
const student = Object.create(person);
student.greet();
// Output: 'Hello!'
  • Object.defineProperty(obj, prop, descriptor): Defines a new property directly on an object or modifies an existing property.
const obj = {};
Object.defineProperty(obj, 'name', {
value: 'Alice',
writable: false });
console.log(obj.name); // 'Alice'
  • Object.defineProperties(obj, props): Defines multiple new properties or modifies existing properties on an object.
const obj = {};
Object.defineProperties(obj, {
name: { value: 'Cormier', writable: false },
age: { value: 30, writable: true } });
console.log(obj.name); // 'Cormier'
  • Object.isExtensible(obj): Determines if an object is extensible (i.e., whether new properties can be added).
const obj = {};
console.log(Object.isExtensible(obj)); // true
Object.preventExtensions(obj);
console.log(Object.isExtensible(obj)); // false
  • Object.isFrozen(obj): Determines if an object is frozen (i.e., not extensible and all properties are non-writable).
const obj = Object.freeze({ name: 'Gregor' });
console.log(Object.isFrozen(obj));
// output: true
  • Object.hasOwn(obj, prop): Returns true if the specified object has the specified property as its own property, even if the property's value is undefined.
const obj = { name: 'Alice' };
console.log(Object.hasOwn(obj, 'name')); // true
console.log(Object.hasOwn(obj, 'age')); // false
  • Object.hasOwnProperty(prop): Determines if an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
const obj = { name: 'Alice' };
console.log(obj.hasOwnProperty('name')); // true
console.log(obj.hasOwnProperty('age')); // false
  • Object.preventExtensions(obj): Prevents new properties from ever being added to an object.
const obj = {};
Object.preventExtensions(obj);
obj.name = 'Khabib'; // Won't be added
console.log(obj); // {}
  • Object.setPrototypeOf(obj, proto): Sets the prototype (the internal [[Prototype]] property) of a specified object.
const proto = { greet() {console.log('Hello!');}};
const obj = {};
Object.setPrototypeOf(obj, proto);
obj.greet(); // 'Hello!'
  • Object.fromEntries(iterable): Transforms a list of key-value pairs into an object.
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.keys(obj));
// Output: ['a', 'b', 'c']
  • Object.getPrototypeOf(obj): Returns the prototype (the internal [[Prototype]] property) of the specified object.
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.values(obj));
// Output: [1, 2, 3]
  • Object.getOwnPropertySymbols(obj): Returns an array of all symbol properties found on the object.
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.entries(obj));
// Output: [['a', 1], ['b', 2], ['c', 3]]
  • Object.getOwnPropertyDescriptor(obj, prop): Returns a property descriptor for a specific property of a given object.
const obj = Object.seal({ a: 1 });
console.log(Object.isSealed(obj));
// Output: true
  • Object.getOwnPropertyNames(obj): Returns an array of all properties found on the object (including non-enumerable properties).
const target = { a: 1 };
const source = { b: 2, c: 3 };
const result = Object.assign(target, source);
console.log(result);
// Output: { a: 1, b: 2, c: 3 }
  • Object.is(value1, value2): Compares if two values are the same.
const obj = { name: 'Khabib' };
Object.freeze(obj);
obj.name = 'Bob'; // This won't change the value
console.log(obj.name); // Output: 'Khabib'
  • Object.getOwnPropertyDescriptors(obj): Returns all own property descriptors of an object.
const obj = { name: 'Alice' };
Object.seal(obj);
obj.name = 'Bob'; // This will update the value
obj.age = 25; // This won't add a new property
console.log(obj); // Output: { name: 'Bob' }

The above is the detailed content of JavaScript Object Methods Example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn