关于javascript的多线程 [英] Multithreading on javascript
本文介绍了关于javascript的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好。
我想为javascript原型提供多线程功能。我的意思是我在我的对象中有一个功能,当我调用它时,它生成一个线程或更好地说web工作者。基本的事情已经完成,但是当我收到来自web worker的消息时,我无法访问引用当前对象的'this'变量,因此它无法访问当前对象的东西。
这是我的代码:
Hello.
I want to give multi threading ability to a javascript prototype. I mean i have a funtion in my object ,when i call it, it generates a thread or better to say web worker. basic things done, but when i receive message from web worker, I cant access 'this' variable that refers to current object ,therefore its no way to access current object stuffs.
Here is my code:
var worker = new Worker('mi.js');
this.p = 1;
worker.postMessage({ test:1});
worker.onmessage = function (e) {
console.log("RETURNED : " + e.data);
//i cant access p on current object.
this.p += 2;
console.log(this.p.toString());
worker.terminate();
};
谢谢
Thank you
推荐答案
this
在该上下文中未定义,如果您确实想要访问父/调用对象,则将此
传递给您的函数 - >功能(e,this)
this
is undefined in that context, if you really want to access the parent/calling object then passthis
to your function ->function(e, this)
这篇关于关于javascript的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文