Download the files, run them on a local server (python -m SimpleHTTPServer
works fine), and open up index.html
.
Last active
August 29, 2015 14:03
-
-
Save bttmly/d7a8482f92e5fe230db2 to your computer and use it in GitHub Desktop.
workers-execution-contex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Workers get a private execution context.</title> | |
</head> | |
<body> | |
<script> | |
var worker = new Worker( "worker.js" ); | |
worker.postMessage( "start" ); | |
worker.addEventListener( "message", function( evt ) { | |
// Look! It the method was successfully invoked in the worker. | |
console.assert( evt.data === "This response generated by Array.prototype.newMethod()" ); | |
// But... | |
console.assert( Array.prototype.newMethod !== undefined, "Array.prototype.newMethod doesn't exist." ); | |
// ... which means the Worker thread and the UI thread don't share global objects like Array.prototype. | |
}, false ); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// all this worker does is add a method to Array.prototoype. | |
// The fact that this method is not available on Array.prototype in the UI thread | |
// indicates that Array.prototype here isn't the same object as Array.prototype there. | |
self.addEventListener( "message", function( evt ) { | |
var arr = [1, 2, 3]; | |
var res; | |
Array.prototype.newMethod = function() { | |
return "This response generated by Array.prototype.newMethod()"; | |
}; | |
// if this doesn't throw an error, the method has been attached as expected. | |
res = arr.newMethod(); | |
self.postMessage( res ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment