Comprehensive Object static methods for property management, object creation, inspection, and manipulation with modern JavaScript object utilities.
Static methods for creating and copying objects with property descriptors and prototype management.
/**
* Object static methods for creation and property management
*/
interface ObjectConstructor {
/**
* Copy properties from source objects to target object
* @param target - Target object to copy properties to
* @param sources - Source objects to copy properties from
*/
assign<T, U>(target: T, source: U): T & U;
assign<T, U, V>(target: T, source1: U, source2: V): T & U & V;
assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
assign(target: object, ...sources: any[]): any;
/**
* Create object with specified prototype and properties
* @param proto - Prototype object or null
* @param propertiesObject - Optional property descriptors
*/
create<T>(proto: T): T;
create<T>(proto: T, propertiesObject: PropertyDescriptorMap & ThisType<any>): T & ThisType<any>;
create(proto: any, propertiesObject?: PropertyDescriptorMap): any;
/**
* Define single property with descriptor
* @param obj - Object to define property on
* @param prop - Property name
* @param descriptor - Property descriptor
*/
defineProperty<T>(obj: T, prop: PropertyKey, descriptor: PropertyDescriptor & ThisType<any>): T;
/**
* Define multiple properties with descriptors
* @param obj - Object to define properties on
* @param properties - Map of property descriptors
*/
defineProperties<T>(obj: T, properties: PropertyDescriptorMap & ThisType<any>): T;
}Usage Examples:
import Object from 'core-js-pure/stable/object';
// Object.assign - Copy properties
const target = { a: 1, b: 2 };
const source1 = { b: 3, c: 4 };
const source2 = { c: 5, d: 6 };
const result = Object.assign(target, source1, source2);
console.log(result); // { a: 1, b: 3, c: 5, d: 6 }
// Object.create - Create with prototype
const personPrototype = {
greet() {
return `Hello, I'm ${this.name}`;
}
};
const person = Object.create(personPrototype, {
name: {
value: 'Alice',
writable: true,
enumerable: true,
configurable: true
},
age: {
value: 30,
writable: true,
enumerable: true,
configurable: true
}
});
console.log(person.greet()); // "Hello, I'm Alice"
// Object.defineProperty - Define single property
const obj = {};
Object.defineProperty(obj, 'hidden', {
value: 'secret',
writable: false,
enumerable: false,
configurable: false
});
console.log(obj.hidden); // 'secret'
console.log(Object.keys(obj)); // [] (not enumerable)Methods for examining object properties, descriptors, and ownership.
interface ObjectConstructor {
/**
* Get property descriptor for own property
* @param obj - Object to examine
* @param prop - Property name
*/
getOwnPropertyDescriptor(obj: any, prop: PropertyKey): PropertyDescriptor | undefined;
/**
* Get all property descriptors for own properties
* @param obj - Object to examine
*/
getOwnPropertyDescriptors<T>(obj: T): {[P in keyof T]: PropertyDescriptor} & PropertyDescriptorMap;
/**
* Get array of own enumerable property names
* @param obj - Object to examine
*/
keys(obj: object): string[];
keys(obj: {}): string[];
/**
* Get array of own enumerable property values
* @param obj - Object to examine
*/
values<T>(obj: { [s: string]: T } | ArrayLike<T>): T[];
values(obj: {}): any[];
/**
* Get array of own enumerable property [key, value] pairs
* @param obj - Object to examine
*/
entries<T>(obj: { [s: string]: T } | ArrayLike<T>): [string, T][];
entries(obj: {}): [string, any][];
/**
* Create object from [key, value] pairs
* @param iterable - Iterable of [key, value] pairs
*/
fromEntries<T = any>(iterable: Iterable<readonly [PropertyKey, T]>): { [k: string]: T };
fromEntries(iterable: Iterable<readonly any[]>): any;
/**
* Get array of own property names (including non-enumerable)
* @param obj - Object to examine
*/
getOwnPropertyNames(obj: any): string[];
/**
* Get array of own symbol properties
* @param obj - Object to examine
*/
getOwnPropertySymbols(obj: any): symbol[];
/**
* Check if object has own property (not inherited)
* @param obj - Object to examine
* @param prop - Property name
*/
hasOwn(obj: object, prop: PropertyKey): boolean;
}Usage Examples:
import Object from 'core-js-pure/stable/object';
// Property inspection
const obj = {
name: 'Alice',
age: 30,
city: 'New York'
};
console.log(Object.keys(obj)); // ['name', 'age', 'city']
console.log(Object.values(obj)); // ['Alice', 30, 'New York']
console.log(Object.entries(obj)); // [['name', 'Alice'], ['age', 30], ['city', 'New York']]
// Object.fromEntries - Create object from entries
const pairs = [['a', 1], ['b', 2], ['c', 3]];
const fromPairs = Object.fromEntries(pairs); // { a: 1, b: 2, c: 3 }
// Transform object using entries/fromEntries
const doubled = Object.fromEntries(
Object.entries(obj)
.filter(([key, value]) => typeof value === 'number')
.map(([key, value]) => [key, value * 2])
);
// Object.hasOwn - Safe property checking
const child = Object.create({ inherited: 'value' });
child.own = 'property';
console.log(Object.hasOwn(child, 'own')); // true
console.log(Object.hasOwn(child, 'inherited')); // false
console.log('inherited' in child); // true (includes inherited)
// Property descriptors
const descriptor = Object.getOwnPropertyDescriptor(obj, 'name');
console.log(descriptor);
// { value: 'Alice', writable: true, enumerable: true, configurable: true }
const allDescriptors = Object.getOwnPropertyDescriptors(obj);
console.log(allDescriptors);
// { name: {...}, age: {...}, city: {...} }Methods for working with object prototypes and inheritance chains.
interface ObjectConstructor {
/**
* Get prototype of object
* @param obj - Object to get prototype of
*/
getPrototypeOf(obj: any): any;
/**
* Set prototype of object
* @param obj - Object to set prototype of
* @param prototype - New prototype object or null
*/
setPrototypeOf<T>(obj: T, prototype: object | null): T;
}Usage Examples:
import Object from 'core-js-pure/stable/object';
// Prototype chain manipulation
const animal = {
speak() {
return 'Some sound';
}
};
const dog = Object.create(animal);
dog.speak = function() {
return 'Woof!';
};
console.log(Object.getPrototypeOf(dog) === animal); // true
// Change prototype at runtime
const cat = { meow() { return 'Meow!'; } };
Object.setPrototypeOf(dog, cat);
console.log(dog.meow()); // 'Meow!'Methods for controlling object mutability and extension.
interface ObjectConstructor {
/**
* Freeze object (prevent all changes)
* @param obj - Object to freeze
*/
freeze<T>(obj: T): Readonly<T>;
/**
* Seal object (prevent property addition/deletion)
* @param obj - Object to seal
*/
seal<T>(obj: T): T;
/**
* Prevent extensions (prevent property addition)
* @param obj - Object to make non-extensible
*/
preventExtensions<T>(obj: T): T;
/**
* Check if object is frozen
* @param obj - Object to check
*/
isFrozen(obj: any): boolean;
/**
* Check if object is sealed
* @param obj - Object to check
*/
isSealed(obj: any): boolean;
/**
* Check if object is extensible
* @param obj - Object to check
*/
isExtensible(obj: any): boolean;
}Usage Examples:
import Object from 'core-js-pure/stable/object';
// Object state management
const obj = { a: 1, b: 2 };
// Prevent extensions
Object.preventExtensions(obj);
obj.c = 3; // Silently fails in non-strict mode
console.log(obj.c); // undefined
console.log(Object.isExtensible(obj)); // false
// Seal object
const sealedObj = Object.seal({ x: 1, y: 2 });
sealedObj.x = 10; // Allowed
delete sealedObj.y; // Silently fails
sealedObj.z = 3; // Silently fails
console.log(Object.isSealed(sealedObj)); // true
// Freeze object
const frozenObj = Object.freeze({ x: 1, y: 2 });
frozenObj.x = 10; // Silently fails
console.log(frozenObj.x); // 1
console.log(Object.isFrozen(frozenObj)); // trueAdditional Object utility methods for comparison and grouping.
interface ObjectConstructor {
/**
* Perform SameValue comparison (like === but handles NaN and -0/+0)
* @param value1 - First value to compare
* @param value2 - Second value to compare
*/
is(value1: any, value2: any): boolean;
/**
* Group array elements into object by key function
* @param items - Array to group
* @param keyFn - Function to determine group key
*/
groupBy<T, K extends PropertyKey>(
items: Iterable<T>,
keyFn: (item: T, index: number) => K
): Partial<Record<K, T[]>>;
}Usage Examples:
import Object from 'core-js-pure/stable/object';
// Object.is - Enhanced equality comparison
console.log(Object.is(NaN, NaN)); // true (unlike === which is false)
console.log(Object.is(-0, +0)); // false (unlike === which is true)
console.log(Object.is(42, 42)); // true
// Object.groupBy - Group array elements
const people = [
{ name: 'Alice', age: 25, city: 'NYC' },
{ name: 'Bob', age: 30, city: 'LA' },
{ name: 'Charlie', age: 25, city: 'NYC' },
{ name: 'Diana', age: 30, city: 'Chicago' }
];
const byAge = Object.groupBy(people, person => person.age);
console.log(byAge);
// {
// 25: [{ name: 'Alice', ... }, { name: 'Charlie', ... }],
// 30: [{ name: 'Bob', ... }, { name: 'Diana', ... }]
// }
const byCity = Object.groupBy(people, person => person.city);
console.log(byCity);
// {
// 'NYC': [{ name: 'Alice', ... }, { name: 'Charlie', ... }],
// 'LA': [{ name: 'Bob', ... }],
// 'Chicago': [{ name: 'Diana', ... }]
// }import Object from 'core-js-pure/stable/object';
// Transform object properties through pipeline
const transformObject = (obj, transforms) => {
return Object.fromEntries(
Object.entries(obj)
.map(([key, value]) => [key, transforms[key]?.(value) ?? value])
);
};
const data = { name: 'alice', age: '25', active: 'true' };
const transforms = {
name: str => str.charAt(0).toUpperCase() + str.slice(1),
age: str => parseInt(str, 10),
active: str => str === 'true'
};
const transformed = transformObject(data, transforms);
// { name: 'Alice', age: 25, active: true }import Object from 'core-js-pure/stable/object';
// Deep clone with property descriptors
const deepCloneWithDescriptors = (obj) => {
if (obj === null || typeof obj !== 'object') return obj;
const clone = Object.create(Object.getPrototypeOf(obj));
const descriptors = Object.getOwnPropertyDescriptors(obj);
for (const [key, descriptor] of Object.entries(descriptors)) {
if (descriptor.value && typeof descriptor.value === 'object') {
descriptor.value = deepCloneWithDescriptors(descriptor.value);
}
Object.defineProperty(clone, key, descriptor);
}
return clone;
};import Object from 'core-js-pure/stable/object';
// Merge configuration objects with validation
const mergeConfig = (defaults, userConfig, validators = {}) => {
const merged = Object.assign({}, defaults, userConfig);
// Validate configuration
for (const [key, validator] of Object.entries(validators)) {
if (Object.hasOwn(merged, key) && !validator(merged[key])) {
throw new Error(`Invalid value for config key: ${key}`);
}
}
return Object.freeze(merged);
};
const defaultConfig = { timeout: 5000, retries: 3, debug: false };
const userConfig = { timeout: 10000, debug: true };
const validators = {
timeout: val => typeof val === 'number' && val > 0,
retries: val => typeof val === 'number' && val >= 0
};
const config = mergeConfig(defaultConfig, userConfig, validators);