[Javascript] yield*

Zhentiw發表於2024-08-26
// Object.prototype[Symbol.iterator] = function() {
//     return Object.values(this)[Symbol.iterator]();
// }

Object.prototype[Symbol.iterator] = function* () {
    // yield* delegates to another generator or iterable object.
    // In this case, it yields each value from the array returned by Object.values(this).
    return yield* Object.values(this);
}

const [a, b] = {a: 1, b: 2}
console.log(a,b)

相關文章