Records and sets are new unchanged JavaScript data types at this time step 2 in the TC39 standards approval process. They are subject to change and are not currently available in any browser or run, but functional implementations should arrive within the next year. They help solve a confusing problem faced by a couple of coders…

Constant changes

Professional JavaScript users will tell you that setting variables const is best practice where possible. It makes variables constant. Values ​​cannot be changed, so you have fewer things.

Unfortunately, const only makes primitive values ​​unchanged (string, number, BigInt, logical, symbol, and undefined). You cannot redefine a matrix or object, but the values ​​and properties they contain butter edit. For example:


const myArray = [1, 2, 3];


myArray[0] = 99;
myArray.push(42);

console.log(myArray); 

myArray = 'change'; 

Correspondingly for objects:


const myObj = { a: 1, b: 2, c: 3 }


myObj.a = 99;
myObj.d = 42;

console.log(myObj); 

myObj = 'change'; 

Object.freeze() the method may help, but only low freezing is applied to the immediate child characteristics of the object:

const myObj = { a: 1, b: 2, c: { v: 3 } }
Object.freeze(myObj);

myObj.a = 99; 
myObj.c.v = 99; 

console.log(myObj); 

Therefore, it is difficult to guarantee that a function does not intentionally or accidentally change the values ​​in a matrix or object. Developers must either wish for the best or pass a cloned version of the variable – (with its own challenges).

Corresponding inequality

More chaos can arise when developers try to make seemingly reasonable object or group comparisons:

const str = 'my string';
console.log( str === 'mystring' );  

const num = 123;
console.log( num === 123 );         

const arr = [1, 2, 3];
console.log( arr === [1, 2, 3] );   

const obj = { a: 1 };
console.log( obj === { a: 1 } );    

Only primitive types can be compared by value. Objects and tables are passed and compared with reference to. Two variables are only equivalent when they point to the same item in memory:

const a = [1, 2];

const b = a;
b.push(3);

console.log( a === b ); 


console.log( a ); 

An in-depth comparison of two objects or tables requires a recursive comparison function to evaluate each value in turn. Even then, you may encounter problems with types, such as dates or activities, that can be saved in different ways.

Tuples: Unchanged table-like data structures

Duplicates are deeply unchanging table-like data structures. They are efficiently combined primitive types identified by a # editor in front of normal table syntax:


const t1 = #[1, 2, 3];
const t2 = #[1, 2, #[3, 4]];

Alternatively, new Tuple.from() method can create a table from a table:


const t3 = Tuple.from( [1, 2, 3] );

Unlike a regular matrix, troops must meet these requirements:

  1. They must not have holes with unset values. For example, #[1,,,4] is not valid.
  2. All they have to do is set primitives, other sets, or records. Types such as tables, objects, or functions are not allowed:
  const t4 = #[ new Date() ]; 
  const t5 = #[1, 2, [3, 4]]; 

Because the sets are primitive, they can be compared in depth to other sets in terms of value:

const t6 = #[1, 2];

console.log( t6 === #[1, 2] ); 

Note that comparisons using less stringent == operator are possible if the double has one value. For example:

const t7 = #[99];

console.log( t7 == #[99] ); 
console.log( t7 == 99 );    
console.log( t7 == '99' );  


console.log( t7 == [99] );  

Records: Unmodified object-oriented data structures

Records are deeply unchanging inherent data structures. Again, they are combined primitive types identified by a # editor in front of normal object syntax:


const r1 = #{ a: 1, b: 2 };
const r2 = #{
  a: 1,
  b: #{ c: 2 }, 
  d: #[ 3, 4 ]  
};

Alternatively, new Record() the constructor can create a record object:



const r3 = Record({ a: 1, b: 2 });

Or Record.fromEntries() the method can create table or double value pairs from a record:



const r4 = Record.fromEntries([
  ['a', 1],
  ['b', 2]
]);

Unlike regular destinations, records must meet the following requirements:

  1. They must use string property names. For example, #{ Symbol(): 1 } is not valid.
  2. They only need to set values ​​with primitives, other doubles, or records. Types such as tables, objects, or functions are not allowed:
  const r5 = #{ 'd': new Date() };   
  const r6 = #{ a: 1, b: { c: 2 } }; 

Records can be compared deeply with other records, and the order of the property is irrelevant:

const r7 = #{ a: 1, b: 2 };

console.log( r7 === #{ b: 2, a: 1 } ); 

Records can only be compared to other records, so a == or === the operator does not matter. However, it is possible to decompress the object keys() and values() for specific comparisons. For example:

const r8 = #{ a: 99 };

console.log( Object.values(r8) == 99 ); 

Unchanged updates

Sets and records may sound like complex computer science terms, but they ultimately allow for robust storage and comparison of immutable data in JavaScript. You can try them today this playgroundor this polyfill, but note that the proposed implementation may change in the coming months.

LEAVE A REPLY

Please enter your comment!
Please enter your name here