Extended ES6 object method Detailed

Read catalog

Object.assign(target, source_1, ···)

Concept: A method for merging the object, the source object (source) of all enumerated attribute, copied to the target object (target)

Features: add properties and methods, clone, merging a plurality of objects as an object, specify a default value for the property

Case:

var sourceObj1 = { 
    name: "ASSIGN" 
} 
var sourceObj2 = { 
    Age: "18 is" 
} 
var target = {}; 
Object.assign (target, sourceObj1, sourceObj2); 
the console.log (target); // {name: " ASSIGN ", Age:" 18 is "} 

// the target object and the source object has attributes with the same name, or a plurality of source objects of the same name attribute, after the attribute overwrite the previous attribute 
var sourceObj3 = { 
    name: " ASSIGN1 " 
} 
var Target1 = {}; 
Object.assign (Target1, sourceObj1, sourceObj3); 
the console.log (Target1); // {name: "ASSIGN1", Age: "18 is"}

//The source object is a string will copy it enumerated attribute, and a Boolean value no effect 

var of sourceString = "ASSIGN" ;
 var TARGET2 = {}; 
Object.assign (TARGET2, of sourceString, to true , 18 is ); 
the console.log (TARGET2); // {0: "a",. 1: "S", 2: "S",. 3: "I",. 4: "G",. 5: "n-"} 

// only string packaging objects, will produce real enumerable predefined attributes, those attributes will be copied [[primitiveValue]] will not be copied Object.assign 
the console.log (Object (of sourceString)); // {0: "a",. 1 : "S", 2: "S",. 3: "I",. 4: "G",. 5: "n-", length:. 6, [[primitiveValue]]: ASSIGN} 

// Object.assign attributes copied is limited, only the attributes of the source object copies itself, does not enumerable attribute copy, the copy is not inherit the properties 
var target3 = {}; 
Object.assign (target3, 
  Object.defineProperty ({},'name' , { 
    Enumerable: to false , 
    value: 'Hello' 
  }), 
  Object.defineProperty ({}, 'Age' , { 
    Enumerable: to true , 
    value: 18 is 
  }) 
) 
the console.log (target3); // {Age : 18 is} 

// Object.assign shallow copy method is practiced, rather than deep copy 
// if the value of a property of the source object is an object, then the resulting copy of the target object is a reference to the object 
var target4 = {}; 
sourceObj4 {name =: {value: "ASSIGN" }}; 
Object.assign (target4, sourceObj4); 
sourceObj4.name.age = 18 is ; 
the console.log (target4);//{name:{value:"assign",age:18}};
</script>

Object.is(value1, value2)(object, descriptors)

Concept: determining whether the two values ​​are the same value

Case:

Object.is(window,window); //true
Object.is({name:"is"},{name:"is"}); //false
Object.is([],[]); //false
Object.is(undefined,undefined); //true
Object.is(null,null);  
Object.is(0, -0);  // false
Object.is(-0, -0);  // true
Object.is(NaN, 0/0);  // true

Object.setPrototypeOf(object)

Concept: modify the properties of an object built [[the Prototype]]

NOTE: This method affects performance, avoid the use, may be used instead of Object.create

var obj = {};
 var OBJ1 = {name: "setPrototypeOf" }; 
Object.setPrototypeOf (obj, OBJ1); 
the console.log (obj.name); // "setPrototypeOf" 

// analog new command 
var F. = function ( ) {};
 var F = Object.setPrototypeOf ({}, F.prototype); 
F.call (F); 
// equivalent 
var F = new new F. ();

Object.getPrototypeOf(object)

Concept: Returns the parameter object prototype

Case:

var F. = function () {}; // constructor 
var F = new new F. (); 
Object.getPrototypeOf (F) === F.prototype // to true 

// empty object prototype Object.prototype 
Object.getPrototypeOf ( } {) === Object.prototype // to true 

// Object.prototype prototype is null 
Object.getPrototypeOf (Object.prototype) === null  // to true 

// function prototype is the Function.prototype 
function F () { } 
Object.getPrototypeOf (F) === the Function.prototype // to true

 

Guess you like

Origin www.cnblogs.com/zhangyaolan/p/11250960.html