JavaScript doesn't have Tuple data type. This example provides custom implementation of Tuple
A Pen by Vlad Bezden
'use strict' | |
class Tuple { | |
constructor(...args) { | |
this._args = args | |
args.forEach((val, index) => { | |
Object.defineProperty(this, `item${index}`, { | |
get: () => val | |
}) | |
}) | |
} | |
*[Symbol.iterator](){ | |
for (const arg of this._args) { | |
yield arg | |
} | |
} | |
} | |
// Usage examples | |
const t = new Tuple('abc', 123) | |
const { | |
item0: a, | |
item1: b | |
} = t | |
console.log(`a: ${a}, b: ${b}`) | |
// -------------------------- | |
for (const value of t){ | |
console.log(value) | |
} | |
// ------------------------- | |
const [first, second] = t | |
console.log(`first: ${first}, second: ${second}`) |
JavaScript doesn't have Tuple data type. This example provides custom implementation of Tuple
A Pen by Vlad Bezden