Complete list of Object methods in JS

Object.keys(obj)

Object.keys(obj): Returns an array consisting of the names of all enumerable properties of an object

const obj = {
    
     a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
console.log(keys); // ['a', 'b', 'c']

Object.values(obj)

Object.values(obj): Returns an array consisting of the values ​​of all enumerable properties of an object .

const obj = {
    
     a: 1, b: 2, c: 3 };
const values = Object.values(obj);
console.log(values); // [1, 2, 3]

Object.entries(obj)

Object.entries(obj): Returns an array of key-value pairs for all enumerable properties of an object .

const obj = {
    
     a: 1, b: 2, c: 3 };
const entries = Object.entries(obj);
console.log(entries); // [['a', 1], ['b', 2], ['c', 3]]

Object.hasOwnProperty(prop)

Object.hasOwnProperty(prop): Determine whether an object has the specified property.

const obj = {
    
     a: 1, b: 2, c: 3 };
const hasProp = obj.hasOwnProperty('a');
console.log(hasProp); // true

Object.assign(target, source1, [source2, …])

Object.assign(target, source1, [source2, …]): Copy the properties of one or more objects to the target object.

const obj1 = {
    
     a: 1 };
const obj2 = {
    
     b: 2 };
const obj3 = {
    
     c: 3 };
const result = Object.assign(obj1, obj2, obj3);
console.log(result); // { a: 1, b: 2, c: 3 }

Object.freeze(obj)

Object.freeze(obj): Freeze an object so that attributes cannot be added, deleted, or modified.

const obj = {
    
     a: 1, b: 2 };
Object.freeze(obj);
obj.c = 3; // 添加属性无效
obj.a = 100; // 修改属性无效
delete obj.b; // 删除属性无效
console.log(obj); // { a: 1, b: 2 }

Object.is(obj1, obj2)

Object.is(obj1, obj2): Determine whether two values ​​are strictly equal .

console.log(Object.is(1, 1)); // true
console.log(Object.is('foo', 'foo')); // true
console.log(Object.is({
    
    }, {
    
    })); // false
console.log(Object.is(+0, -0)); // false
console.log(Object.is(NaN, NaN)); // true

Object.getOwnPropertyDescriptor(obj, prop)

Object.getOwnPropertyDescriptor(obj, prop): Get the property descriptor of the specified property .

const obj = {
    
     value: 123 };
const descriptor = Object.getOwnPropertyDescriptor(obj, 'value');
console.log(descriptor); // { value: 123, writable: true, enumerable: true, configurable: true }

Object.setPrototypeOf(obj, prototype)

Object.setPrototypeOf(obj, prototype): Set the prototype of the object (that is, set its inheritance relationship).

const obj1 = {
    
    };
const obj2 = {
    
     value: 123 };
Object.setPrototypeOf(obj1, obj2);
console.log(obj1.__proto__ === obj2); // true

Object.create(proto, [propertiesObject])

Object.create(proto, [propertiesObject]): Create a new object using the specified prototype .

const obj1 = {
    
     a: 1 };
const obj2 = Object.create(obj1, {
    
     b: {
    
     value: 2 }});
console.log(obj2.a); // 1
console.log(obj2.b); // 2

Object.fromEntries(iterable)

Object.fromEntries(iterable): Create a new object from an iterable object .

const entries = [['a', 1], ['b', 2]];
const obj = Object.fromEntries(entries);
console.log(obj); // { a: 1, b: 2 }

Object.defineProperties(obj, props)

Object.defineProperties(obj, props): Defines a descriptor for one or more properties .

const obj = {
    
    };
Object.defineProperties(obj, {
    
    
  a: {
    
     value: 1 },
  b: {
    
     value: 'hello' }
});
console.log(obj.a); // 1
console.log(obj.b); // 'hello'

Object.defineProperty(obj, prop, descriptor)

Object.defineProperty(obj, prop, descriptor): Define a property descriptor .

const obj = {
    
    };
Object.defineProperty(obj, 'a', {
    
    
  value: 123,
  writable: false,
  enumerable: true,
  configurable: false
});
console.log(obj.a); // 123
obj.a = 456; // 不可写,赋值无效
console.log(obj.a); // 123
for (let key in obj) {
    
     console.log(key); } // 可枚举
delete obj.a; // 不可配置,删除无效
console.log(obj.a); // 123

Object.getOwnPropertyNames(obj)

Object.getOwnPropertyNames(obj): Returns all property names on an object (excluding Symbol).

const obj = {
    
     a: 1, b: 2 };
const keys = Object.getOwnPropertyNames(obj);
console.log(keys); // ['a', 'b']

Object.getOwnPropertySymbols(obj)

Object.getOwnPropertySymbols(obj): Returns all Symbol property names on an object .

const sym = Symbol('key');
const obj = {
    
     a: 1, [sym]: 10 };
const symbols = Object.getOwnPropertySymbols(obj);
console.log(symbols); // [Symbol('key')]

Object.isExtensible(obj)

Object.isExtensible(obj): Determines whether an object is extensible (whether new properties can be added).

const obj1 = {
    
     a: 1 };
const obj2 = {
    
     b: 2 };
console.log(Object.isExtensible(obj1)); // true
Object.preventExtensions(obj1);
console.log(Object.isExtensible(obj1)); // false
console.log(Object.isExtensible(obj2)); // true

Object.preventExtensions(obj)

Object.preventExtensions(obj): Prevents an object from being extended (no new properties can be added).

const obj = {
    
     a: 1 };
Object.preventExtensions(obj);
obj.b = 2;
console.log(obj); // { a: 1 }

Object.isSealed(obj)

Object.isSealed(obj): Determines whether an object is sealed (no more attributes can be added or deleted).

const obj = Object.freeze({
    
     a: 1 });
console.log(Object.isSealed(obj)); // true

Object.seal(obj)

Object.seal(obj): Prevents an object from being closed (attributes can no longer be added or deleted, but attribute values ​​can be modified).

const obj = {
    
     a: 1 };
Object.seal(obj);
obj.b = 2; // 无法添加新属性
delete obj.a; // 无法删除属性
obj.a = 100; // 可以修改属性值
console.log(obj); // { a: 100 }

Guess you like

Origin blog.csdn.net/Jet_Lover/article/details/130176103