简体   繁体   English

为什么在 javascript 生成器中产生突变的 object 会在转换为数组时导致不同的行为?

[英]Why does yielding a mutated object in a javascript generator result in different behavior when converted to array?

I have a generator function foo written in JavaScript.我有一个用 JavaScript 编写的生成器 function foo

function* foo() {
    const bar = { a: 0, b: 0 };
    yield bar;

    bar.a += 1;
    yield bar;

    bar.b += 10;
    yield bar;
}

The function foo takes no parameter but keeps an internal object bar that is used for yielding. function foo不带参数,但保留用于屈服的内部 object bar The function can yield 3 times, each time returning a different value. function 可以产生 3 次,每次返回不同的值。

The expected behavior is observed when calling next().value on the resulting object.在生成的 object 上调用next().value时会观察到预期的行为。

const baz = foo()
console.log(baz.next().value)   // => { a: 0, b: 0 }
console.log(baz.next().value)   // => { a: 1, b: 0 }
console.log(baz.next().value)   // => { a: 1, b: 10 } 

However, when using the Array.from method to create an array from the resulting iterator, a strange behavior is observed instead.但是,当使用Array.from方法从生成的迭代器创建数组时,会观察到一种奇怪的行为。

console.log(Array.from(foo()))  // => [ { a: 1, b: 10 }, { a: 1, b: 10 }, { a: 1, b: 10 } ]

The final object is returned for every yield instead of return the value each time yield was called.最终的 object 为每个 yield 返回,而不是在每次调用yield时返回值。 This behavior happens in both node 10.13 and node 12.7?这种行为发生在节点 10.13 和节点 12.7 中? Why is this happening?为什么会这样? Shouldn't the results be the same for both cases?两种情况的结果不应该相同吗?

Since in javascript objects are passed by reference you are modifying the same object after each yield and in the end you have an array with 3 references to the same object in memory in its final state. Since in javascript objects are passed by reference you are modifying the same object after each yield and in the end you have an array with 3 references to the same object in memory in its final state.

To get an array with different objects you would have to create new objects on each yield, for example using object spread syntax.要获得具有不同对象的数组,您必须在每个产量上创建新对象,例如使用 object 扩展语法。

 function* foo() { let bar = { a: 0, b: 0 }; yield bar; bar = {...bar, a: bar.a + 1} yield bar; bar = {...bar, b: bar.b + 10} yield bar; } const baz = foo() console.log([...baz])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 数组没有以某种方式发生突变 - Javascript array is not somehow mutated 为什么这个 JavaScript 代码中的数组没有被改变? - Why isn't the array being mutated in this JavaScript code? 为什么我的JavaScript对象通过返回转换为字符串? - Why does my JavaScript object get converted to a string by returning it? 为什么`{} + []`在Javascript中从`a = {} + []`返回不同的结果? - Why does `{} + []` return a different result from `a = {} + []` in Javascript? 为什么Javascript EventLoop有不同的结果? - Why does the Javascript EventLoop have different result? 为什么当状态发生变异时 mapStateToProps 返回空对象? - Why mapStateToProps return empty object when state has been mutated? 有人可以向我解释一下,为什么此代码结果不同?(javascript对象和数组) - can somene explain me, why this code result is different?(javascript object and array) JSON与JavaScript对象数组。 为什么第二种方法的结果与第一种方法不同? 以及如何实现呢? - JSON vs. JavaScript object array. Why is the second approach different in result than the first? And how to achieve this? 为什么JavaScript对相同的行为有不同的声明? - Why does JavaScript have different declarations for the same behavior? 为什么声明 vs 不声明 JavaScript for Loop 中的变量的行为不同? - Why the behavior for declaring vs does not declare the variable in JavaScript for Loop is different?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM