Object方法

117 阅读3分钟

keys()

Object.keys() :返回一个数组,其元素是字符串

const arr = ["a", "b", "c"];
console.log(Object.keys(arr)); // ['0', '1', '2']

const obj = { 10: "b", 12: "e", 3: "k" }
console.log(Object.keys(obj));// ['3', '10', '12']

// getFoo 是一个不可枚举的属性
const myObj = Object.create(
  {},
  {
    getFoo: {
      value() {
        return this.foo;
      },
    },
  },
);
myObj.foo = 1;
console.log(Object.keys(myObj)); // ['foo']

values()

Object.values() :一个包含了给定对象的自有可枚举字符串键属性值的数组。

const obj = { foo: "bar", baz: 42 };
console.log(Object.values(obj)); // ['bar', 42]

// 使用数字键时,将按键的数字顺序返回值
const arrayLikeObj2 = { 100: "a", 2: "b", 7: "c" };
console.log(Object.values(arrayLikeObj2)); // ['b', 'c', 'a']

// getFoo 是一个不可枚举的属性
const myObj = Object.create(
  {},
  {
    getFoo: {
      value() {
        return this.foo;
      },
    },
  },
);
myObj.foo = "bar";
console.log(Object.values(myObj)); // ['bar']

defineProperties()

Object.defineProperties(obj, props) :在一个对象上定义新的属性或修改现有属性,并返回该对象。

obj

在其上定义或修改属性的目标对象

props

属性对象,其属性值为属性描述符

数据描述符

enumerbale/configurable/writable等特性(描述符)均默认为false。

访问器描述符

setter/getter等特性(描述符)均默认为undefined。

const obj = {};
Object.defineProperties(obj, {
  property1: {
    value: true,
    writable: true,
  },
  property2: {
    value: "Hello",
    writable: false,
  },
  // 等等……
});

create()

Object.create(proto):以一个现有对象作为原型,创建一个新对象。

proto

如果 proto 既不是 null也不是 Object则抛出此错误

const person = {
  isHuman: false,
  printIntroduction: function () {
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
  },
};
const me = Object.create(person);
me.name = 'Matthew';
me.isHuman = true; 
me.printIntroduction();  //"My name is Matthew. Am I human? true"

assign

Object.assign(target, ...sources):将一个或者多个源对象中所有可枚举的自有属性复制到目标对象,并返回修改后的目标对象。

target

需要应用源对象属性的目标对象,修改后将作为返回值。

sources

一个或多个包含要应用的属性的源对象。

const obj = { a: 1 };
const copy = Object.assign({}, obj);
console.log(copy); // { a: 1 }

const obj1 = { a: 0, b: { c: 0 } };
const obj2 = Object.assign({}, obj1);
console.log(obj2); // { a: 0, b: { c: 0 } }

obj1.a = 1;
console.log(obj1); // { a: 1, b: { c: 0 } }
console.log(obj2); // { a: 0, b: { c: 0 } }

obj2.a = 2;
console.log(obj1); // { a: 1, b: { c: 0 } }
console.log(obj2); // { a: 2, b: { c: 0 } }

getOwnPropertyDescriptor()

Object.getOwnPropertyDescriptor(obj, prop):

获取指定对象的自身属性描述符。自身属性描述符是指直接在对象上定义(而非从对象的原型继承)的描述符。如果指定的属性存在于对象上,则返回其属性描述符,否则返回 undefined

obj

要查找其属性的对象。

prop

要检索其描述的属性的名称或 Symbol

数据描述符

enumerbale/configurable/writable等特性(描述符)true。

访问器描述符

没有setter/getter等特性(描述符)均默认为undefined。

let o, d;

o = {
  get foo() {
    return 17;
  },
};
d = Object.getOwnPropertyDescriptor(o, "foo");
console.log(d);
// {
//   configurable: true,
//   enumerable: true,
//   get: [Function: get foo],
//   set: undefined
// }

o = { bar: 42 };
d = Object.getOwnPropertyDescriptor(o, "bar");
console.log(d);
// {
//   configurable: true,
//   enumerable: true,
//   value: 42,
//   writable: true
// }

o = { [Symbol.for("baz")]: 73 };
d = Object.getOwnPropertyDescriptor(o, Symbol.for("baz"));
console.log(d);
// {
//   configurable: true,
//   enumerable: true,
//   value: 73,
//   writable: true
// }

o = {};
Object.defineProperty(o, "qux", {
  value: 8675309,
  writable: false,
  enumerable: false,
});
d = Object.getOwnPropertyDescriptor(o, "qux");
console.log(d);
// {
//   value: 8675309,
//   writable: false,
//   enumerable: false,
//   configurable: false
// }

hasOwn()

Object.hasOwn(obj, prop):如果指定的对象中直接定义了指定的属性,则返回 true;否则返回 false。

obj

要测试的 JavaScript 实例对象。

prop

要测试属性的 String 类型的名称或者 Symbol。

const example = {};
Object.hasOwn(example, "prop"); // false——目标对象的属性 'prop' 未被定义

example.prop = "exists";
Object.hasOwn(example, "prop"); // true——目标对象的属性 'prop' 已被定义

example.prop = null;
Object.hasOwn(example, "prop"); // true——目标对象本身的属性存在,值为 null

example.prop = undefined;
Object.hasOwn(example, "prop"); // true——目标对象本身的属性存在,值为 undefined