構造関数Objectの属性と方法についてまとめます.

19355 ワード

@(JavaScriptベース)
JavaScript内蔵対象:Object
Objectオブジェクト(またはObject構築関数)
  :
-----------------------------------------------------------------------------------
Object.length:
      1
-----------------------------------------------------------------------------------
Object.propotype:
      Object     
        Object.propotype.__proto__   Object          , null
        Object.propotype.constructor   Object          ,     Object
            Object.prototype       :
                                :
                configurable:
                             configurable   true  ,            ,
                        delete    ,   writable             
                        false
                enumerable:
                            enumerable true ,                 。
                        false
                           :
                value:
                           。    undefined。
                writable:
                            writable true ,             
                        false
                           :
                get:
                            getter    ,     getter    undefined。
                                。    undefined。
                      :      
                set:
                            setter    ,     setter    undefined。
                              ,              。    undefined
-----------------------------------------------------------------------------------
  :
-----------------------------------------------------------------------------------
Object.create(proto[, propertiesObject]):
      :
                           
      :
        proto:
                      
        propertiesObject:
                    undefined,                 
                  Object.defineProperties()      
       :
                         
      :
        const obj={name:'lee'};
        const newObj=Object.create(obj);
        console.log(obj);       // {name:'lee'}
        console.log(obj.name);  // "lee"
        const newObject=Object.create(obj,{
            age:{
                value:18
            },
            hobby:{
                value:'read'
            }
        });
        console.log(newObject); // {age: 18, hobby: "read"}
-----------------------------------------------------------------------------------
Object.defineProperty(obj, prop, descriptor)
      :
                          
      :
        obj
        prop
        descriptor:
                 
       :
                 
      :
        const o={
            name:'lee',
            _age:12
        };
        //    
        Object.defineProperty(o,'name',{
            value:'lee',
            writable : true,
            configurable : true,
            enumerable : true
        });    {name:"lee"}
        Object.defineProperty(o,'age',{
            get:function(){
                return this._age;
            },
            set:function(value){
                console.log(value,'set')
                return value*2;
            },
            enumerable:true,
            configurable:true
        });
        console.log(o.age);  // 12
        o.age=18;            // 18 set
        console.log(o.age);  // 12
        //   :           set     
-----------------------------------------------------------------------------------
Object.defineProperties(obj, props):
      :
                           
      :
        obj
        props
       :
                 
      :
        const obj={};
        Object.defineProperties(obj,{
            name:{
                value:"lee",
                writable : true,
                configurable : true,
                enumerable : true
            },
            age:{
                value:18,
                writable : true,
                configurable : true,
                enumerable : true
            }
        });   // {name: "lee", age: 18}
-----------------------------------------------------------------------------------
Object.is(value1, value2):
      :
                 。   NaN     (  == ===  )
                  ,      :
                  undefined
                  null
                  true      false
                                     
                      
                     
                 +0
                 -0
               NaN
                  NaN          
      :
        value1
        value2
       :
        boolean 
      :
        Object.is('foo', 'foo');     // true
        Object.is(window, window);   // true
        Object.is('foo', 'bar');     // false
        Object.is([], []);           // false
        //   
        Object.is(0, -0);            // false
        Object.is(-0, -0);           // true
        Object.is(NaN, 0/0);         // true
-----------------------------------------------------------------------------------
Object.assgin( target,...sources ):
    :
                                  ,
    :
      target:
              
    sources:
            
     :
            
      :
        const obj={},
            source={a:1},
             arr=[2,3];
        Object.assign(obj,source,arr);  // {0: 2, 1: 3, a: 1}
      :
                                 
-----------------------------------------------------------------------------------     
Object.preventExtensions(obj):
      :
                 
      :
        obj:
                       
       :
                 
      :
        const obj={};
        const newObj=Object.create(obj);
        newObj.name='lee';
        newObj.age=18;
        Object.preventExtensions(newObj);
        Object.getOwnPropertyDescriptors(newObj);
        // {
        //  age:{
        //      value: 18, 
        //      writable: true, 
        //      enumerable: true, 
        //      configurable: true   //   preventExtensions              
        //  },
        //  name:{
        //      value: "lee", 
        //      writable: true, 
        //      enumerable: true,
        //       configurable: true
        //     }
        // }
        newObj.hobby='read';
        console.log(newObj);  // {name: "lee", age: 18}
        delete newObj.name;
        console.log(newObj);  // {age: 18}
        obj.name='panda';
        console.log(newObj.name);  // 'lee'
-----------------------------------------------------------------------------------
Object.seal(obj):
      :
                 ,          。
                         ,               (configurable)      (false),               。
               (writable)   (true)            
      :
        obj
       :
                
      :
        const obj={};
        const newObj=Object.create(obj);
        newObj.name='lee';
        newObj.age=18;
        Object.seal(newObj);
        Object.getOwnPropertyDescriptors(newObj);
        // {
        //  age:{
        //      value: 18, 
        //      writable: true, 
        //      enumerable: true, 
        //      configurable: false   //   seal              
        //  },
        //  name:{
        //      value: "lee", 
        //      writable: true, 
        //      enumerable: true,
        //       configurable: false
        //  }
        // }
        newObj.hobby='read';
        console.log(newObj);  // {name: "lee", age: 18}
        delete newObj.name;
        console.log(newObj);  // {name: "lee", age: 18}
        newObj.name='panda';
        console.log(newObj.name);  // 'panda'
-----------------------------------------------------------------------------------
Object.freeze(obj):
      :
            :               ,
                    ,               ,          
      :
        obj
       :
              
      :
        const obj={};
        const newObj=Object.create(obj);
        newObj.name='lee';
        newObj.age=18;
        Object.freeze(newObj);
        Object.getOwnPropertyDescriptors(newObj);
        // {
        //  age:{
        //      value: 18, 
        //      writable: false,    //   freeze            
        //      enumerable: true, 
        //      configurable: true   //   freeze            
        //  },
        //  name:{
        //      value: "lee", 
        //      writable: false, 
        //      enumerable: true,
        //       configurable: true
        //  }
        // }
        newObj.hobby='read';
        console.log(newObj);  // {name: "lee", age: 18}
        delete newObj.name;
        newObj.name='panda';
        console.log(newObj);  // {name: "lee", age: 18}
        newObj.name='panda';
        console.log(newObj.name);  // 'lee'


        //     
        function deepFreeze(obj){
            var propNames = Object.getOwnPropertyNames(obj);
            propNames.forEach(function(name) {
                var prop = obj[name];
            if (typeof prop == 'object' && prop !== null)
              deepFreeze(prop);
            });
           return Object.freeze(obj);
        };
        const o={person:{name:'lee'}};
        deepFreeze(o);o.person.name='zz';
        console.log(o.person.name);  // 'lee'
-----------------------------------------------------------------------------------
Object.isExtensible(obj):
      :
                 ,              
             ,       :            。
              __proto__         。
        Object.preventExtensions,Object.seal   Object.freeze                 
      :
        obj
       :
        boolean 
      :
        const obj={};
        Object.isExtensible(obj); // true

        // ...        .
        Object.preventExtensions(obj);
        Object.isExtensible(obj); //  false
        
        //           .
        Object.seal(obj);
        Object.isExtensible(obj); // false
        
        //           .
        Object.freeze(obj);
        Object.isExtensible(obj); // false
-----------------------------------------------------------------------------------
Object.isSealed(obj):
      :
                  ,    true,     false
                       ,                   (        )   
      :
        obj
       :
        boolean
      :
        const obj={name:'lee'};
        Object.preventExtensions(obj);
        obj.age=18;
        console.log(obj)    
-----------------------------------------------------------------------------------
Object.isFrozen(obj):
      :
                  
                       ,           ,
               (   getter setter         )      
      :
        obj
       :
        boolean
      :
        const obj={};
        Object.isSealed(obj); // false

        // ...        .
        Object.preventExtensions(obj);
        Object.isSealed(obj); //  true
        
        //           .
        Object.seal(obj);
        Object.isSealed(obj); // true
        
        //           .
        Object.freeze(obj);
        Object.isSealed(obj); // true
-----------------------------------------------------------------------------------
           
Object.getOwnPropertyDescriptor
Object.getOwnPropertyDescriptor(obj, prop):
      :
                   
      :
        obj
        prop
       :
                     ,           (property descriptor),
             undefined
      :
        
-----------------------------------------------------------------------------------
Object.getOwnPropertyDescriptors
Object.getOwnPropertyDescriptors(obj):
      :
                   
      :
        obj
       :
                        ,          ,      
      :
        const obj={};
        const newObj=Object.create(obj);
        newObj.name='lee';
        newObj.age=18;
        Object.getOwnPropertyDescriptors(newObj);
        // {
        //  age:{
        //      value: 18, 
        //      writable: true,    
        //      enumerable: true, 
        //      configurable: true   
        //  },
        //  name:{
        //      value: "lee", 
        //      writable: true, 
        //      enumerable: true,
        //       configurable: true
        //  }
        // }
              :
  :
        :
                 ,              
                       
        :
                   ,                   (        ,     (writable)   (true)            )   
        :
                          ,           ,        ,                  、   。(          ,    )
            ,           
    
    :
      ECMAScript 5   ,                    :
    
    for...in   
                             。(   +   )
    Object.keys(o)
              o     (      )           。(   )
    Object.getOwnPropertyNames(o)
             ,       o        (       )   。(    ,      )
    Object.getOwnPropertySymbols(obj):
             ,                 。(    )
    
    Object.entries(obj):
                     [key, value]  

      :
    const o={
            sex:'boy',
            weight:'60kg'
        };
        let age=Symbol('age');
        const newObj=Object.create(o)
        Object.defineProperties(newObj,{
            'name':{
                value:'lee',
                writable:true,
                enumerable:true,
                configurable:true,
            },
            'hobby':{
                value:'read',
                writable:true,
                enumerable:true,
                configurable:true,
            },
            [age]:{
                value:18,
                writable:true,
                enumerable:false,
                configurable:true,
            },
            'friend':{
                value:'Jack',
                writable:true,
                enumerable:false,
                configurable:true,
            },
        });
        const arr=[];
        for(let v in newObj){
            arr.push(v)
        };
        console.log(arr);       //  ["name", "hobby", "sex", "weight"]
        const arr2=Object.keys(newObj);
        console.log(arr2);      //  ["name", "hobby"]
        const arr3=Object.getOwnPropertyNames(newObj);
        console.log(arr3);      //  ["name", "hobby", "friend"]
        const arr4=Object.getOwnPropertySymbols(newObj);        
        console.log(arr4);      //  [Symbol(age)]
        const arr5=Object.entries(newObj);
        console.log(arr5);      //   [["name", "lee"],["hobby", "read"]]
-----------------------------------------------------------------------------------
Object.keys(obj):
      :
                                ,
                       for...in                 
        (            for-in               )
      :
        obj
       :
                              
      :
        const obj={name:'lee'};
        const newObj=Object.create(obj);
        newObj.age=18;
        console.log(Object.keys(newObj));  // ['name']
        const arr=[];
        for(let v in newObj){
            arr.push(v);
        }
        console.log(arr);  // ['name','age']
-----------------------------------------------------------------------------------
Object.getOwnPropertyNames(obj):
      :
              ,                       
      :
        obj
       :
                           
      :
        const arr=['a','b','c','d'];
        Object.getOwnPropertyNames(arr);  // ["0", "1", "2", "3", "length"]
        const arr={0:'a',1:'b',2:'c'};
        console.log(Object.getOwnPropertyNames(arr));  // ["0", "1", "2"]
        const obj={name:'lee',age:18};
        console.log(Object.getOwnPropertyNames(obj));  //  ["name", "age"]
-----------------------------------------------------------------------------------
Object.getOwnPropertySymbols(obj):
      :
              ,                 
      :
        obj
       :
                      Symbol      
      :
         const obj={};
        let a=Symbol('a');
        let b=Symbol('b');
        obj[a]='name';
        obj[b]='age';
        obj.hobby='read';
        Object.getOwnPropertySymbols(obj);  //  [Symbol(a), Symbol(b)]
-----------------------------------------------------------------------------------
Object.values(obj):
      :
                       
      :
        obj
       :
        Object.values()      ,                
      :
        const obj={name:'lee',age:18};
        Object.values(obj);   // ["lee", 18]
-----------------------------------------------------------------------------------
Object.entries(obj):
      :
                      [key, value]  
      :
        obj
       :
                         
      :
        const obj={name:"lee",age:18};
        Object.entries(obj));  //[['name','lee'],['age',18]]
            
-----------------------------------------------------------------------------------
Object.getPrototypeOf(obj):
      :
                   
      :
        obj
       :
               。        ,    null
      :
        const proto=null;
        const obj=Object.create(proto);   // {}
        Object.getPrototypeOf(obj)==null;    // true
-----------------------------------------------------------------------------------
Object.setPrototypeOf(obj, prototype):
      :
                     (  ,   [[Prototype]]  )         null
      :
        obj:
                     
        prototype:
                   (       null)
       :
                 
      :
        const obj={};
        const proto={name:'lee',age:18};
        Object.setPrototypeOf(obj,proto);
        console.log(obj.__proto__);  // {name: "lee", age: 18}
-----------------------------------------------------------------------------------
Object.prototype
  :
-----------------------------------------------------------------------------------
Object.prototype.constructor
         ,           
-----------------------------------------------------------------------------------
Object.prototype.__proto__ 
                ,       
-----------------------------------------------------------------------------------
  :
-----------------------------------------------------------------------------------
Object.prototype.hasOwnProperty(prop):
      :
                ,               ,            。
      :
        prop
       :
        boolean 
-----------------------------------------------------------------------------------
Object.prototype.isPrototypeOf(obj)
      :
               ,                  
      :
        obj
       :
        boolean 
-----------------------------------------------------------------------------------

Object.prototype.propertyIsEnumerable()
           ,         ECMAScript [[Enumerable]] attribute 。
Object.prototype.toLocaleString()
     toString()  。
Object.prototype.toString()
          。
Object.prototype.valueOf()
-----------------------------------------------------------------------------------