Object.assign plurality of combined properties of an object if the object has the same name attribute, the value of the object behind the front cover.

 

 

 

 

Object.assign () method is used to enumerate all the attribute values ​​are copied from one or more source object to the target object. It will return to the target object.


<script>
	const target = { a: 1, b: 2 };
	const source = { b: 4, c: 5 };

	const returnedTarget = Object.assign(target, source);

	console.log(target);
	// expected output: Object { a: 1, b: 4, c: 5 }

	console.log(returnedTarget);
	// expected output: Object { a: 1, b: 4, c: 5 }
</script>




grammar
Object.assign(target, ...sources)

parameter

target audience.

sources source object.

Returns the value of the target object.

description
If the target object property with the same key, the attributes are covered by the source object property. Properties of the source object will similarly cover behind the front of the properties of the source object.

Object.assign method simply copy source and the object itself may be enumerated attributes to the target object. So it will call the relevant getter and setter methods to use the source object [[Get]] and the target object [[Set]]. Accordingly, it is assigned attributes, rather than just copying or define new attributes. If the merge source contains getter, which may make it unsuitable for the new property incorporated into the prototype. In order to define the attribute (which may include enumeration of) to the prototype, use Object.getOwnPropertyDescriptor () and Object.defineProperty ().

String type and Symbol types of properties will be copied.

In the case of an error, for example, if the property can not write, can cause TypeError, if you add a property before any raises an error, you can change the target object.

Note, Object.assign does not throw an error in that the source object is null or undefined time.


Quote: https: //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

  

 

 

 

 

Here it must be a reference library can jQuery.js

$ .Extend () merging a plurality of objects, the object behind the front object attribute property overrides
/*$.extend( target [, object1 ] [, objectN ] )
$.extend( [deep ], target, object1 [, objectN ] )

deep optional. Boolean type indicates whether the object is the depth of the merger, the default is false. If true, the property of the same name and a plurality of objects are also object, the "object" of the property will be merged.
target Object type audience member attributes of other objects to be attached to the object.
object1 optional. Object type of the first object to be merged.
objectN optional. Object type N-th objects are merged. * /

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<Title> novice tutorial (runoob.com) </ title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
 
<div id="log"></div>
<script>
$(function () { 
	There object1 = {
		apple: 0,
		banana: {weight: 52, price: 100},
		cherry: 97
	};
	var object2 = {
		banana: {price: 200},
		durian: 100
	};
	/ * Object2 incorporated into object1 in * /
	$.extend(object1, object2);
	var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) {
		was arr = [];
		$.each(obj, function(key, val) {
			var next = key + ": ";
			next += $.isPlainObject(val) ? printObj(val) : val;
			arr.push( next );
		});
		return "{ " +  arr.join(", ") + " }";
	};
	$("#log").append( printObj(object1) );
})
</script>
 
</body>
</html>

  

Guess you like

Origin www.cnblogs.com/Knowledge-is-infinite/p/12093212.html