javascript深刻淺出 對象

 

對象結構web

    屬性標籤:json

        可寫    writablethis

        可配置    configurablespa

        可遍歷    enumerableprototype

        獲取/設置code

            get/setorm

1
2
3
4
5
6
7
8
9
var  obj = {
     $name :  null ,
     get  name(){
         return  this .$name+ '-fix'
     },
     set  name(val){
         this .$name = val;
     }
};

    對象標籤:對象

        [[proto]]
繼承

            原型實現繼承ip

        [[class]]

            獲取對象類型

1
2
3
function  getType(o) {
     return  Object .prototype.toString.call(o).slice( 8 ,- 1 );
}

        [[extensible]]

            isExtensible

                是否可擴展

                阻止擴展    Object.preventExtensions(obj)

            isSealed

                是否可擴展、可配置

                阻止擴展和配置    Object.seal(obj);

            isFrozen

                是否可擴展、可配置、可寫

                阻止可擴展、可配置、可寫    Object.freeze(obj);

    值:

        value


對象建立

    字面量:

    new構造方法/原型鏈:

    Object.create(字面量):

        字面量做爲原型

對象操做

    屬性操做:

        屬性讀寫

            不存在寫會報錯

            不存在讀不報錯

        屬性刪除

            可刪除和不存在的返回true

            不可刪的會報錯

        屬性檢查

            in    是否存在,包含原型鏈

1
'x'  in  obj

            hasOwnProperty    是否存在,不包含原型鏈

1
obj.hasOwnProperty( 'x' )

            propertyIsEnumberable    是否能夠遍歷

1
obj.propertyIsEnumerable( 'x' )

        屬性標籤訂義

            字面量定義

                writable,configurable,enumerable默認爲true

            使用defineProperty

                writable,configurable,enumerable默認爲false

        獲取屬性標籤描述

1
Object .getOwnPropertyDescriptor(對象, '屬性' )


    序列化:

        json序列化

1
var  json_str = JSON.stringify(obj);

        json反序列化

1
var  str = JSON.parse(json_str);

        自定義

1
2
3
4
5
6
7
to_json : {
     a :  1 ,
         b :  2 ,
         toJSON :  function  () {
         return  this .a+ this .b;
     }
}

        注意

            值爲undefined 會去掉

            值爲NaN,Infinity 轉爲null

            new Date 轉爲 UTC格式

    對象轉基本類型:

        toString

        valueOf

    ​    ​    ​valueOf 優先toString

    ​    ​    ​定義valueOf

1
2
3
test_o.valueOf =  function  () {
     return  'value ' ;
};
相關文章
相關標籤/搜索