美文网首页
复习:Object

复习:Object

作者: 黄同学2019 | 来源:发表于2020-04-24 11:42 被阅读0次

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 等

相关文章

网友评论

      本文标题:复习:Object

      本文链接:https://www.haomeiwen.com/subject/icpxwhtx.html