Js 中你一定用到的 {}.APi

77 阅读3分钟

Object.create()

使用指定原型和可选属性创建一个新对象。

const person = {
  firstName: 'John',
  lastName: 'Doe'
};

const student = Object.create(person, {
  grade: { value: 'A' }
});

Object.keys()

返回一个由所有可枚举的字符串属性组成的数组。

const person = {
  firstName: 'John',
  lastName: 'Doe'
};

const keys = Object.keys(person);
console.log(keys); // ['firstName', 'lastName']

Object.values()

返回给定对象的所有可枚举属性的值的数组。

const person = { firstName: 'John', lastName: 'Doe' };
const values = Object.values(person);
console.log(values); // ['John', 'Doe']

Object.entries()

返回一个由所有可枚举属性的 [key, value] 数组组成的数组。

const person = {
  firstName: 'John',
  lastName: 'Doe'
};

const entries = Object.entries(person);
console.log(entries); // [['firstName', 'John'], ['lastName', 'Doe']]

Object.fromEntries()

将键值对数组转换为对象。

const entries = [['firstName', 'John'], ['lastName', 'Doe']];
const person = Object.fromEntries(entries);
console.log(person); // { firstName: 'John', lastName: 'Doe' }

Object.assign()

将一个或多个源对象的所有可枚举属性复制到目标对象,并返回目标对象。

const person = {
  firstName: 'John',
  lastName: 'Doe'
};

const employee = {
  jobTitle: 'Developer'
};

const result = Object.assign(employee, person);
console.log(result); // { jobTitle: 'Developer', firstName: 'John', lastName: 'Doe' }

该 API 会影响源数据

Object.defineProperty()

定义一个新属性或修改现有属性的特性。

const person = {};

Object.defineProperty(person, 'firstName', {
  value: 'John',
  writable: false,
  enumerable: true,
  configurable: true
});
// defineProperty是es6之前的一个api,vue2曾使用它来实现对象拦截,但有一次缺陷,如数组无法拦截。现在使用proxy代替。

除此之外,还可以定义 getset 方法来控制属性的读取和写入行为。属性描述符对象中的 getset 分别代表了获取和设置该属性时所调用的函数,该 API 会影响源数据

类似API:Object.definePropertys()

Object.freeze()

冻结一个对象,使其不可更改。

const person = {
  firstName: 'John',
  lastName: 'Doe'
};

Object.freeze(person);

person.firstName = 'Jane'; // 更新失败,因为对象已被冻结

console.log(person); // { firstName: 'John', lastName: 'Doe' }

该 API 会影响源数据

Object.seal()

密封一个对象,防止添加新属性并将所有现有属性标记为不可配置。

const person = {
  firstName: 'John',
  lastName: 'Doe'
};

Object.seal(person);

delete person.firstName; // 删除失败,因为对象已被密封

person.middleName = 'Mark'; // 添加失败,因为对象已被密封

console.log(person); // { firstName: 'John', lastName: 'Doe' }

该 API 会影响源数据

Object.is()

比较两个值是否相同(与“===”等效)。

console.log(Object.is('foo', 'foo')); // true

console.log(Object.is({}, {})); // false,因为两个对象在内存中不同

Object.constructor()

返回创建该对象实例的构造函数。

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const john = new Person('John', 'Doe');
console.log(john.constructor); // [Function: Person]

Object.hasOwnProperty()

检查指定属性是否为对象的自有属性,而不是通过原型链继承来的属性。

const person = { firstName: 'John', lastName: 'Doe' };

console.log(person.hasOwnProperty('firstName')); // true
console.log(person.hasOwnProperty('toString')); // false,因为 toString 是从 Object.prototype 继承的

Object.isPrototypeOf()

检查一个对象是否为另一个对象的原型。

function Person() {}

const john = new Person();

console.log(Person.prototype.isPrototypeOf(john)); // true

Object.valueOf()

返回对象的原始值,通常与 toString 方法一起使用。

console.log((4).valueOf()); // 4

Object.toString()

返回对象的字符串表示形式。

const person = { firstName: 'John', lastName: 'Doe' };
console.log(person.toString()); // [object Object]
// 这里应用场景还可以通过call查看其他类型
Object.prototype.toString.call([]) // [object Array]
// 以及基础类型
Object.prototype.toString.call(1)// [object Number]

Object.toSource()

返回对象的源代码表示形式。

const person = { firstName: 'John', lastName: 'Doe' };
console.log(person.toSource()); // ({firstName:"John", lastName:"Doe"})

Object.getOwnPropertyDescriptors()

返回指定对象所有自有属性的描述符对象(包括 Symbol 类型的属性)。

let key = Symbol()
const person = {
  firstName: 'John',
  lastName: 'Doe',
  [key]:'Symbol'
};

const descriptors = Object.getOwnPropertyDescriptors(person);
console.log(descriptors);
/*
  {
    firstName: {
      value: "John",
      writable: true,
      enumerable: true,
      configurable: true
    },
    lastName: {
      value: "Doe",
      writable: true,
      enumerable: true,
      configurable: true
    },
    Symbol():{
    "value": "Symbol",
    "writable": true,
    "enumerable": true,
    "configurable": true
	}
  }
*/