I have the following:
function NumberFormatter(){
...
function helper(){
...
var tempvar;
...
}
function format(num){
...
helper()
...
}
}
//there is a single instance of FT
var FT = new NumberFormatter()
FT.format(123)
The state of the object is not changed.
In a multithreading context, does this code fail if format()
is called from two different places almost simultaniously?
Is there a simple way to lock the object or is it better to hold 1000 instances?
Answer: (summarizing all posted here...)
No, the code won't fail (because there is no real multithreading in JavaScript)
No way to lock an object and you don't need to
You can have one instance. No need to create 1000 instances
For those who don't believe, the proofcode:
<script type='text/javascript'>
function get_random_color() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.round(Math.random() * 15)];
}
return color;
}
setInterval('document.getElementById("a").style.backgroundColor=get_random_color()', 10)
//*** setInterval() stops when heavy calculations are done ***//
document.getElementById("b").onclick = function(){
for(var i=0;i<10000000; i++){
Math.atan2(Math.round(1000))
}
}
</script>