Application instance analysis of ES6 Object method extension

This example describes the application of the ES6 Object method extension. Share to everyone for reference, as follows:

Some extensions and optimizations for objects in ES6, the following is simple summary

IS method judgment two Whether the object is all

 Console.log (Object.is ({}, {}), {} ===}); // False Falseconsole.log (Object.is (nan, nan), nan === nan); // true, falseconsole.log (Object.is (+0, -0), +0 === -0); / / false, true   
ASSIGN method is used for copy of the object attribute

If two parameters, the second parameter Enumerable attributes copy to the first parameter object, limited to enumerable properties, if there are multiple parameters, then copy

var obj = { }; var obj2 = Object.assign (Obj, {name: ‘joh’}, {age: 10}; console.log (Obj === Obj2, obj); // true, {name: ‘joh’, Age: 10} console.log (Object.is (obj, obj2)); // true
  Copy copy of the same attribute  
const default_options = {name: “joh”}; function test (opts) {let options =Object.assign ({}, default_options, opts); console.log (options);} test (); // {name: “joh”} test ({name: “LILY”, AGE: 10}); // {Name: “Lily”, Age: 10}

Symbol attributes same copy
   VAR SKEY1 = SYMBOL ( 'test'); var skey2 = symbol ('test'); var obj = {}; object.assign (Obj, {name: 'joh'}, {age: 10}, {[SKEY1]: 'I am Test1 '}, {[SKEY2]:' I am test2 '}); // Copy // Verify Symbol copy Success Console.log (Obj [SKEY1]); // i am TestConsole.log (OBJ); // { Name: "JOH", AGE: 10, SYMBOL (TEST): "I am Test1", SYMBOL (TEST): "I am Test2"} 

Object method: keys, getownpropertynames, getownpropertyMbols, getownpropertyDescriptor application
  Class a {constructor () {this.name = 'joh';} [Symbol (' Fullnamea ')] () {} getName () {}} Class B EXTENDS A {Constructor () {super (); this.age = 22;} [Symbol ('fullnameb')] () {} getage () {} b.Prototype.getColor = function () {}; var b = New b (); // Note: You can only get [yourself enumerate], but you can't get the [Protocol chain attribute (such as method) + symbols attribute] console.log (Object.keys (b)) ; // ["Name", "age"] // Note: You can get [Enumerable Properties], but you can't get the [Prototizes on the prototype chain + Symbols attribute] console.log (Object.getownPropertynames (b)) ; // ["" "" "" "AGE"] // How to use Keys not get the prototype chain unusual properties [Creative method created by syntax] Console.log (Object.keys (B.Prototype)) ; // ["getColor"] // Incoming Prototype using getownpropertyNames You can get the properties of the prototype chain console.log (b.getoPropertyNames (B.Prototype)); // ["Constructor", "GetColor", "Getage" ] // You can get your Symbols property through getownpropertyMbols, but you will not get inherited Symbol property console.log (Object.getoWnPropertySymbols (b.prototype)); // [Symbol (Fullnameb)] // Get itself enumeration property including Inherited, Note: The method created by the syntax of the Class is inefficient, but through the later B.Prototype. It is an enumerable for (Let Key IN B) {Console.log (key); // sequentially outputs Name and Age and getColor} // Description Object Test Enumerable Console.log (Object.GetownPropertyDescriptor (B.prototype, 'getColor')); console.log (Object.getOwnPropertyDescriptor (B.prototype, 'getColor') enumerable);. // trueconsole.log (Object.getOwnPropertyDescriptor (B.prototype, 'getAge'). Enumerable); // false  
More about JavaScript Related Contents View this Topic: “JavaScript Object-Oriented Telings”, “JavaScript Find Algorithm Skills Summary”, ” JavaScript error and debugging skill summary Helped.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment