Object相关方法

  • getOwnPropertySymbols

    获取对象中所有Symbol类型的key

    const a = Symbol('a');
    let obj = {};
    obj[a] == 11;
    Object.getOwnPropertySymbols(a);
    > Symbol(a)
    
  • getOwnPropertyNames

    返回一个由指定对象的所有自身属性的属性名(包括不可枚举属性但不包括Symbol值作为名称的属性)组成的数组

    let scc= {age:11}
    Object.getOwnPropertyNames(scc)
    > ["age"]
    
  • Object.defineProperty(obj, prop, descriptor)

    在对象上定义属性,并描述属性的状态,比如configurable: false, writable: true, enumerable: true, value: '张三'

    let a= {};
    Object.defineProperty(a,age,{
        writable:false,//是否可以被改写,不会报错。
        enumerable:false,//是否可以被枚举
        configurable:true,//是否可以被删除
        value:22//属性的值,
        get:func,//取值时会触发
        set:func//设置值时会触发
    })
    
  • Object.defineProperties

    与上面的区别就是,第二个参数同时定义属性名,和描述

    Object.defineProperties(obj, {
    name: {
        value: '张三',
        configurable: false,
        writable: true,
        enumerable: true
    },
    age: {
            value: 18,
            configurable: true
        }
    })
    
  • Object.preventExtensions(obj);

    设置对象为不能扩展,即不能添加新属性.

  • Object.isExtensible(obj);

    设置对象为可以扩展,即是上面的取反。

  • obj.hasOwnProperty(prop)

    判断对象是否拥有该属性

    let a = { age:11 };
    a.hasOwnProperty('age')
    > true
    
  • Object.getOwnPropertyDescriptors

    返回对象的所有的属性的描述
    let a = {age:1} Object.getOwnPropertyDescriptors(a);

  • Object.getOwnPropertyDescriptor

    获取对象指定的属性的描述

    let a = {age:1}
    Object.getOwnPropertyDescriptor(a,'age');
    
  • Object.getPrototypeOf(a)

    获取对象的原型

  • isPrototypeOf

    获取后面的那个是不是在前面那个的原型链上

    let a =function(){}
    let b = new a();
    a.prototype.isPrototypeOf(b);
    > true
    
  • Object.setPrototypeOf(a,{age:11})

    设置对象的proto,想当于,a.proto = {age:11}

  • Object.getPrototypeOf(a)

    获取对象的proto

后面还会补充....

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 此文章用于归纳Object的所有方法 在JavaScript中,object是所有对象的基础(原型链的顶端),所以...
    moonburn阅读 667评论 0 5
  • 本篇主要介绍JS中常用Object的属性方法。 delete 操作 in 运算符 obj.hasOwnProper...
    boySpray阅读 2,034评论 0 2
  • 来自:参 考 原 文 对象是由多个名/值对组成的无序的集...
    wyude阅读 1,276评论 1 7
  • defineProperty() 学习书籍《ECMAScript 6 入门 》 Proxy Proxy 用于修改某...
    Bui_vlee阅读 674评论 0 1
  • 1. 2. 3.画渣上色时候是非常紧张的……因为一不小心就毁了…… 4.生活丢三落四,画画也不例外。画一半不知道笔...
    sleep_eat阅读 1,451评论 46 30