1、Object.entries()
Object.entries () can be a key object to traverse an array out, and the results for ... in ... the same, but does not traverse the prototype property
Examples a: --- incoming object
const obj = { foo: 'bar', baz: 'abc' }; console.log(Object.entries(obj)); // [['foo', 'bar'], ['baz', 'abc']]
Example b: --- incoming array
const arr = [1, 2, 3]; console.log(Object.entries(arr)); // [['0', 1], ['1', '2'], ['2', '3']]
Example c: --- incoming array containing objects
const arr1 = [{ a: 1 }, 2, 3]; console.log(Object.entries(arr1)); // [['0', { a: 1 }], ['1', '2'], ['2', '3']]
Examples of d: --- all incoming array of objects
const arr2 = [{ a: 1 }, { b: 2 }, { c: 3 }]; console.log(Object.entries(arr2)); // [['0', { a: 1 }], ['1', { b: 2 }], ['2', { c: 3 }]]
Examples e: --- incoming string
const str = '123'; console.log(Object.entries(str)); // [['0', '1'], ['1', '2'], ['2', '3']]
Examples f: --- incoming numbers, floating point numbers
const num = 123; console.log(Object.entries(num)); // [] const float1 = 12.3; console.log(Object.entries(float1)); // []
Examples g: --- into the Object Map
const obj2 = { foo: 'bar', baz: 'abc' }; console.log(Object.entries(obj2)); // [['foo', 'bar'], ['baz', 'abc']] const map = new Map(Object.entries(obj2)); console.log(map); // Map {'foo' => 'bar', 'baz' => 'abc'}
2、Object.assign(target,source1,source2,...)
This method only copy source attribute object itself, not copied inherited attributes.
Object.assign method of implementation is shallow copy, rather than a deep copy. That is, the value of a property if the source object is an object, then get a copy of the target object is a reference to the object. Property of the same name will be replaced.
Object.assign only copies the value, the value if you want to copy is a function of the value, then evaluated and then copied.
Object.assign can be used to handle an array, but the array will treated as an object.
{X = target const: 0, Y:. 1 }; const Source = {X:. 1, Z: 2, Fn: {Number:. 1 }}; the console.log (Object.assign (target, Source)); // target {x: 1, y: 1, z: 2, fn: {number: 1}} // attribute with the same name will be overwritten target.fn.number = 2 ; the console.log (Source) // Source {X:. 1 , z: 2, fn: { number: 2}} // copy the object reference
function the Person () { the this .name =. 1 }; Person.prototype.country = 'China' ; var Student = new new the Person (); student.age = 29 ; const Young = {name: 'Zhang' }; Object.assign (Young, Student); // Young {name: 'Zhang', Age: 29} // copy only own properties, can not be copied prototype
Object.assign ([1, 2, 3], [4, 5]) // the array as an object to handle // [4, 5, 3] // {0: 1, 1: 2, 2: 3 } {0: 4, 1: 5}
Examples of a: Combine Objects
var first = { name: "Bob" }; var last = { lastName: "Smith" }; var person = Object.assign(first, last); console.log(person);/*{ name: 'Bob', lastName: 'Smith' }*/
Examples of b: clone
var obj = { person: "Bob Smith"}; var clone = Object.assign({}, obj); console.log(clone);/*{ person: 'Bob Smith' }*/ var test=null; var test1=Object.assign({},test); console.log(test1);/*{}*/ var test2=undefined; var test4=Object.assign({},test2); console.log(test4);/*{}*/
3、Object.create(prototype,descriptors)
var newObj = Object.create(null, { size: { value: "large", enumerable: true }, shape: { value: "round", enumerable: true } }); document.write(newObj.size + "<br/>");/*large*/ document.write(newObj.shape + "<br/>");/*round*/ document.write(Object.getPrototypeOf(newObj));/*null*/
var firstLine = { x: undefined, y: undefined }; var secondLine = Object.create(Object.prototype, { x: { value: undefined, writable: true, configurable: true, enumerable: true }, y: { value: undefined, writable: true, configurable: true, enumerable: true } }); document.write("first line prototype = " + Object.getPrototypeOf(firstLine));/*first line prototype = [object Object])*/ document.write("<br/>"); document.write("second line prototype = " + Object.getPrototypeOf(secondLine));/*first line prototype = [object Object]*/
4、Object.keys(obj) && Object.values(obj)
5、Object.freeze() && Object.isFrozen()