Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6复习-Object #25

Open
Jsmond2016 opened this issue Mar 20, 2022 · 0 comments
Open

ES6复习-Object #25

Jsmond2016 opened this issue Mar 20, 2022 · 0 comments
Labels
面试 面试题系列

Comments

@Jsmond2016
Copy link
Owner

MDN 上所有 Object 方法

属性

  • Object.prototype.constructor
  • Object.prototype._proto_

判断对象,返回 boolean

  • Object.is(val1, val2) 比较两个值是否相同
  • Object.isExtensible()
  • Object.isFrozen() 是否已经冻结。
  • Object.isSealed() 是否已经密封
  • Object.prototype.hasOwnProperty()
  • Object.prototype.isPrototypeOf()
  • Object.prototype.propertyIsEnumerable()

返回 array

  • Object.entries() 遍历 key 和 value,返回一个 Map
const obj = { foo: 'bar', baz: 42 };
console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
  • Object.keys() 只遍历 key
  • Object.values() 只遍历 value

返回 string

  • Object.prototype.toLocaleString()
  • Object.prototype.toString()

返回对象

  • Object.fromEntries() 将 map 等 键值对列表转换为一个对象
  • Object.prototype.valueOf() 返回原始值
  • Object.assign(targetObj, otherObj)
  • Object.create() 将创建的对象挂载在 Object._proto_ 上,区别如下
var obj = Object.create({'name': 'Tom'})
console.log(obj)  // {} 展开看到 name 在 _proto_ 里面

var obj2 = {'name': 'Tom'}
console.log(obj2) // {name: "Tom"}

获取对象

  • Object.getOwnPropertyDescriptor() 属性的描述进行检索
  • Object.getOwnPropertyDescriptors()
  • Object.getOwnPropertyNames() 类似拿到 对象的 key 和 数组的 下标和 length
  • Object.getOwnPropertySymbols()
  • Object.getPrototypeOf()

更改对象

  • Object.defineProperties(obj, props)
  • Object.defineProperty(obj, props, configs)
  • Object.setPrototypeOf() mdn不建议使用
  • Object.preventExtensions() 不可拓展,成为一个冻结对象 freeze
  • Object.freeze() 冻结对象,不能使用 set, get 操作对象
  • Object.seal() 密封对象

最常用

  • Object.assign(targetObj, otherObj)
  • Object.keys() 只遍历 key
  • Object.values() 只遍历 value
  • Object.create()
  • Object.prototype.toString()

其他拓展

  • for... in 遍历一个对象的除Symbol以外的可枚举属性,通常遍历对象的 key 和数组的下标
  • for... of 对 enumerable对象操作,包括数组和对象, Set, Map 等
@Jsmond2016 Jsmond2016 added the 面试 面试题系列 label Mar 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
面试 面试题系列
Projects
None yet
Development

No branches or pull requests

1 participant