[英]Does array.push copies deeply?
I'm trying to make array of workers so I wrote this code. 我正在尝试使一组工人工作,所以我编写了这段代码。
for(var c=0;c<core;c++){
let worker = new Worker("Html/Worker.js");
worker.postMessage({core:core,W:width,H:height,id:c,px:px,py:py,pz:pz,yaw:yaw,pitch:pitch,D:D,fov:fov});
worker.onmessage=(e)=>{
let gap = this.sH/core*e.data[2];
this.frameBuffer.set(e.data[0],Math.floor(gap)*this.sW*4);
//alert(e.data);
this.depthBuffer.set(e.data[1],Math.floor(gap)*this.sW);
}
this.threads.push(worker);
}
I wonder if it's able to copy worker without multiple initialization (like the code below) 我想知道它是否能够在不进行多次初始化的情况下复制worker(如下面的代码)
let worker = new Worker("Html/Worker.js");
for(var c=0;c<core;c++){
worker.postMessage({core:core,W:width,H:height,id:c,px:px,py:py,pz:pz,yaw:yaw,pitch:pitch,D:D,fov:fov});
worker.onmessage=(e)=>{
let gap = this.sH/core*e.data[2];
this.frameBuffer.set(e.data[0],Math.floor(gap)*this.sW*4);
this.depthBuffer.set(e.data[1],Math.floor(gap)*this.sW);
}
this.threads.push(worker);
}
does array.push causes deep copying? array.push会导致深度复制吗?
No, Array.push
does not cause deep copying. 不, Array.push
不会引起深度复制。
this.threads.push(worker)
will not modify the worker
you are passing to it but will just add it as-is to the array this.threads
. this.threads.push(worker)
不会修改您要传递给它的worker
,而只是将其原样添加到this.threads
数组中。
This means in your second example there will be only one worker which receives core
many messages and have a single message listener. 这意味着在您的第二个示例中,将只有一个工作线程接收core
许多消息并具有单个消息侦听器。
Please notice that in both examples you should not access c
in your onmessage
callback because at the time of receiving messages c
will be equal to core
. 请注意,在这两个示例中,都不应在onmessage
回调中访问c
,因为在接收消息时c
等于core
。 If you need it in the callback, declare it with let c
instead of var c
to have access to the value at the time of creating the callback ( explanation ). 如果需要在回调中使用它,请使用let c
而不是var c
进行声明,以在创建回调时访问该值 ( 说明 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.