Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
JavaScript
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
Posted

1 solution

this is undefined in that context, if you really want to access the parent/calling object then pass this to your function -> function(e, this)
 
Share this answer
 
Comments
ali_heidari_ 8-May-15 5:21am    
i tried this, but problem is it wouldnt access to functions, example this.doSomething();
Mehdi Gholam 8-May-15 5:33am    
Run under a debugger and see what "this" you are getting.
ali_heidari_ 18-May-15 8:13am    
is it possible to access funtctions of a prototype in a webworker? I made a server ajax control , a part of it, is javascript prototype, and in this prototype i need an animation , What you suggest for such this animation that needs, performance in memory,cpu, show smoothly, and there is about hundereds objects to animate asynchronously! thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900