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,033評論 0 2
  • 來自:參 考 原 文 對象是由多個名/值對組成的無序的集...
    wyude閱讀 1,276評論 1 7
  • defineProperty() 學習書籍《ECMAScript 6 入門 》 Proxy Proxy 用于修改某...
    Bui_vlee閱讀 672評論 0 1
  • 1. 2. 3.畫渣上色時候是非常緊張的……因為一不小心就毀了…… 4.生活丟三落四,畫畫也不例外。畫一半不知道筆...
    sleep_eat閱讀 1,449評論 46 30