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']
Copy after login
Copy after login
-
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]
Copy after login
Copy after login
-
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]]
Copy after login
Copy after login
-
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
Copy after login
Copy after login
-
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 }
Copy after login
Copy after login
-
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'
Copy after login
Copy after login
-
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' }
Copy after login
Copy after login
-
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!'
Copy after login
-
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'
Copy after login
-
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'
Copy after login
-
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
Copy after login
-
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
Copy after login
-
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
Copy after login
-
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
Copy after login
-
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); // {}
Copy after login
-
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!'
Copy after login
-
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']
Copy after login
Copy after login
-
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]
Copy after login
Copy after login
-
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]]
Copy after login
Copy after login
-
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
Copy after login
Copy after login
-
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 }
Copy after login
Copy after login
-
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'
Copy after login
Copy after login
-
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' }
Copy after login
Copy after login
The above is the detailed content of JavaScript Object Methods Example. For more information, please follow other related articles on the PHP Chinese website!