Comprehensive Guide to JavaScript Object Methods and Their Usage
This article provides an in‑depth overview of JavaScript's Object methods—including defineProperty, defineProperties, assign, create, freeze, seal, preventExtensions, fromEntries, hasOwnProperty, hasOwn, getOwnPropertyNames, getOwnPropertySymbols, getPrototypeOf, setPrototypeOf, isPrototypeOf, getOwnPropertyDescriptor, getOwnPropertyDescriptors, propertyIsEnumerable, keys, values, entries, is, and groupBy—explaining their purpose, parameters, behavior, and offering practical code examples for each.
JavaScript objects are fundamental for data handling, and the language offers a rich set of methods to manipulate object properties, control mutability, and inspect metadata. This guide walks through each method, its signature, typical use‑cases, and provides runnable code snippets.
Object.defineProperty – Property Interception
Used by frameworks like Vue 2 to create reactive properties. It takes a target object, a property name, and a descriptor (configurable, enumerable, writable, value, get, set). The descriptor defines how the property behaves.
const obj = { name: "iceCode" };
const newObj = Object.defineProperty(obj, "age", {
value: "12",
configurable: true,
writable: true,
enumerable: true,
});
console.log(newObj, obj, newObj === obj);If a descriptor contains both value and get/set , a TypeError is thrown.
Object.defineProperties – Batch Definition
Allows defining multiple property descriptors at once, returning the target object.
const obj = { name: "iceCode" };
const newObj = Object.defineProperties(obj, {
name: { value: "icedCode", writable: true, enumerable: true },
age: { value: 18, writable: true, enumerable: true },
});
console.log(newObj);Object.assign – Shallow Copy
Copies enumerable own properties from source objects to a target object, returning the target. Nested objects are shared.
const obj = { name: "iceCode" };
const age = { age: 18 };
const my = { interest: ["唱", "跳", "rap", "篮球"] };
const newObj = Object.assign(obj, age, my);
console.log(newObj, newObj === obj);
obj.age = 24;
obj.interest.push("足球");
console.log(obj);Object.create – Prototype‑Based Creation
Creates a new object with a specified prototype and optional property descriptors.
const proto = { name: "iceCode" };
const newObj = Object.create(proto, {
age: { value: 12, writable: true, enumerable: true, configurable: true },
});
console.log(newObj);Object.freeze – Immutable Object
Prevents extensions, deletions, and modifications of existing properties. The operation is irreversible.
const obj = { name: "iceCode" };
const frozen = Object.freeze(obj);
// Any further mutation attempts throw in strict modeObject.isFrozen – Check Freeze Status
Returns true if the object is frozen, including edge cases where an object is non‑extensible and all properties are non‑configurable.
Object.seal – Seal Object
Prevents adding or deleting properties but allows modification of existing ones.
const obj = { name: "iceCode" };
const sealed = Object.seal(obj);
sealed.age = 18; // allowed
// delete sealed.name; // throws in strict modeObject.isSealed – Check Seal Status
True when the object cannot be extended and all existing properties are non‑configurable.
Object.preventExtensions – Disallow Extensions
Stops new properties from being added while keeping existing ones mutable.
const obj = { name: "iceCode" };
const noExt = Object.preventExtensions(obj);
noExt.name = "iced"; // allowed
delete noExt.name; // allowedObject.isExtensible – Check Extensibility
Returns false for frozen, sealed, or prevented‑extension objects.
Object.fromEntries – Build Object from Key‑Value Pairs
Converts an iterable of [key, value] pairs (e.g., a Map or array) into an object.
const arr = [["2", "4"], ["name", "iceCdoe"], ["age", 18]];
const map = new Map(arr);
const obj = Object.fromEntries(map);
console.log(obj);Object.prototype.hasOwnProperty – Own Property Check
Returns true only if the property exists directly on the object, not on its prototype.
Object.hasOwn – Modern Own‑Property Check
Standardized in newer environments; works like hasOwnProperty but as a static method.
Object.getOwnPropertyNames / getOwnPropertySymbols
Return arrays of an object's own property names (including non‑enumerable) and Symbol‑keyed properties, respectively.
Object.getPrototypeOf & Object.setPrototypeOf
Retrieve or change an object's prototype. Changing the prototype at runtime is slow and discouraged.
Object.isPrototypeOf – Prototype Chain Test
Instance method to test whether an object appears in another object's prototype chain.
Object.getOwnPropertyDescriptor(s)
Return the descriptor(s) for a specific property or all own properties.
Object.propertyIsEnumerable – Enumerability Test
Indicates whether a property can be enumerated in a for…in loop.
Object.keys, Object.values, Object.entries
Return arrays of own enumerable property names, values, or [key, value] pairs. For arrays, these correspond to indices.
Object.is – Same‑Value Comparison
More precise than === , correctly handling NaN and distinguishing +0 from -0 .
valueOf, toString, toLocaleString
Inherited from Object.prototype ; usually overridden by built‑in types for meaningful conversion.
Object.groupBy – Group Iterable Elements (Experimental)
Groups elements of an iterable based on a callback return value. Compatibility is limited (Node < 16.9, Chrome < 117).
const data = [
{ name: "i", sex: "男", age: 12 },
{ name: "ie", sex: "男", age: 12 },
{ name: "iq", sex: "女", age: 12 },
// ...more items
];
const grouped = Object.groupBy(data, v => v.sex);
console.log(grouped);In practice, most projects rely on Object.keys for simple enumeration, while the more advanced methods are useful in specialized scenarios such as immutability control or meta‑programming.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.