Created
November 17, 2013 13:46
-
-
Save ushiboy/7513614 to your computer and use it in GitHub Desktop.
workerのpostMessageの検証。Function混じらなければ文字列化の必要なかった。
This file contains 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
var worker = new Worker('worker.js'); | |
worker.addEventListener('message', function(evt) { | |
var meta = evt.data, | |
key; | |
for (key in meta) { | |
console.log(key + ':' + meta[key]); | |
} | |
}, false); | |
worker.postMessage({ | |
s : 'test', | |
n : 12345, | |
b : true, | |
d : new Date(), | |
a : [1,2,3], | |
o : { | |
n : 12345 | |
} | |
}); | |
try { | |
worker.postMessage({ | |
s : 'test', | |
n : 12345, | |
b : true, | |
f : function() {}, | |
d : new Date(), | |
a : [1,2,3], | |
o : { | |
n : 12345 | |
} | |
}); | |
} catch(e) { | |
console.error(e.name, e.message); | |
} |
This file contains 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
addEventListener('message', function(evt) { | |
var data = evt.data, | |
key, type, value, | |
toString = Object.prototype.toString, | |
meta = {}; | |
for (key in data) { | |
if (data.hasOwnProperty(key)) { | |
value = data[key]; | |
type = typeof(value); | |
if (type === 'object') { | |
if (Array.isArray(value)) { | |
type = 'array'; | |
} else if(toString.apply(value) === '[object Date]') { | |
type = 'date'; | |
} | |
} | |
meta[key] = type; | |
} | |
} | |
postMessage(meta); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment