ES5 object to the extension of static methods

1. Object.create (prototype [, descriptors]): create a new object

1) Create a new object to the specified object as the prototype

2) Specify the new property, and property described

value: specified value

writable: identifies the current property value is a modifiable default is true

get: the callback function to obtain the current attribute value

set: for monitoring the current property value change callback

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        'use strict'
 //  1. Object.create(prototype,[descriptors])
    //   Role: to specify the object as the prototype of a new object is created
     //    specify the new attribute for the new object, and property description
     //    value: Specifies the value
     //    Writable identifies whether the current property can be modified, the default is false
     //    configurable: identifying whether a current property can be deleted, the default is to false
     //    Enumerable: identifying whether the attribute can be used for in this enumeration, defaults to false 



      var obj = {username: ' Damu ' , Age: 30 }
       var OBJ1 = {}  
       // Object uppercase O
  //      OBJ1 the Object.create = (obj);
  //      the console.log (OBJ1) 

      // OBJ1 add the attribute
     //     OBJ1 the Object.create = (obj, {
     //         Sex: {
    //             value: 'M'
     //         } 

    //     })
     //     the console.log (obj1.sex) 

       // modify the properties of obj1
     //     obj1 = the Object.create (obj, {
     //         Sex: {
     //             value: 'M',
     //             Writable: to true
     //         } 

    //     })
     //   console.log (obj1.sex)
     //   obj1.sex = 'female'
     //   console.log (obj1.sex); 


      //    delete obj1 attribute
     //     OBJ1 the Object.create = (obj, {
     //         Sex: {
     //            value:'男',
    //            writable:true,
    //            configurable:true
    //        }

    //    })
    //  console.log(obj1.sex)
    //  obj1.sex='女'
    //  console.log(obj1.sex)
    //  delete obj1.sex;
    //  console.log(obj1)
  
              
     //    for in 枚举
        obj1=Object.create(obj,{
            sex:{
                value:'',
                writable:true,
                configurable:true,
                enumerable:true
            }

        })

        for(var i in obj1){
            console.log(i)
        }



    </script>
</body>
</html>
Object create

2. Object.defineProperties (object, descriptors): is defined as a plurality of extended attributes specified object

<! DOCTYPE HTML> 
<HTML lang = " EN " > 
<head> 
  <Meta charset = " UTF-. 8 " > 
  <title> 03_Object extended </ title> 
</ head> 
<body> 
<-! 
The ES5 extended to Object several static methods commonly used 2: 
1 . Object.create (prototype, [descriptors])
   * role: to create a new object of the specified object as the prototype
   * specify a new attribute for the new object, and the described property 
    value: Specifies the value 
    writable: identifies whether the current property value is modified, the default is false 
    the Configurable: identifies whether the current attributes can be deleted by default false 
    Enumerable: identify the current property if you can use for in enumeration defaults to false 

2 Object..defineProperties(object, descriptors)
  * Role: define a plurality of extended attributes for the specified object
   * GET : used to obtain the current attribute worthy callback
   * SET : modify the current property worth callback triggered, and the argument value is the modified
   * accessor property: the setter , one for getters stored value, a value for
 -> 
<Script type = " text / JavaScript " > // Object.defineProperties (Object, descriptors) var obj2 = { 
        firstName: ' Curry ' , 
        lastName: ' Stephen ' 
    }; 
    Object.defineProperties (obj2, { 
        the fullName: { GET : function () {
                 return the this
 
    
    
             .firstName + '-' + this.lastName
            },
            set : function (data) {
                var names = data.split('-');
                this.firstName = names[0];
                this.lastName = names[1];
            }
        }
    });
    console.log(obj2.fullName);
    obj2.firstName = 'tim';
    obj2.lastName = 'duncan';
    console.log(obj2.fullName);
    obj2.fullName = 'kobe-bryant';
    console.log(obj2.fullName);
</script>
</body>
</html>
View Code

3.

Two methods of the object itself
* get propertyName () {} is used to obtain the current attribute value callback
* set propertyName () {} is used to monitor the current attribute value change callback

<! DOCTYPE HTML> 
<HTML lang = " EN " > 
<head> 
    <Meta charset = " UTF-. 8 " > 
    <title> the Title </ title> 
<head /> 
<body> 
<-! 
    Two object itself method
     * gET propertyName () {} is used to obtain the current attribute value callback
     * SET propertyName () {} is used to monitor the current attribute value change callbacks
 -> 
<Script type = ' text / JavaScript ' >
     var obj = { 
        firstName: ' Kobe ' , 
        lastName:'bryant',
        get fullName(){
            return this.firstName + ' ' + this.lastName
        },
        set fullName(data){
            var names = data.split(' ');
            this.firstName = names[0];
            this.lastName = names[1];
        }
    };
    console.log(obj.fullName);
    obj.fullName = 'curry stephen';
    console.log(obj.fullName);

</script>
</body>
</html>
View Code

 

Guess you like

Origin www.cnblogs.com/hack-ing/p/12005414.html