本来パースエラーになるべきだと思うけど何でならないんだろ… https://t.co/siXwrJ5J0c
— tkr (@kgtkr) 2018年4月12日
カッコで括ると式になるので
— not safe for work liked (@wreulicke) 2018年4月12日
正しくobject + array になる
{}ではなく、Object.createメソッドとnullで本当に空っぽのオブジェクトを作成した場合、配列(array)との足し算がどうなるのか気になったので。
結果を見て、「JavaScript: The Good Parts」は結構前の本だけど、良いパーツを使うことは大切だなぁとあらためて感じたり。
コード(Emacs)
HTML5
<pre id="output0"></pre> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample.js"></script>
JavaScript
let pre0 = document.querySelector('#output0'),
btn0 = document.querySelector('#run0'),
btn1 = document.querySelector('#clear0'),
p = (x) => pre0.textContent += x + '\n';
let output = () => {
let a = {} + [],
b = ({} + []);
// {} + [] はconsole.logだと0になった
p({} + []);
p(a);
p(b);
// オブジェクトでも、Object.createで作成するとまた違った結果に。
let obj = Object.create(null),
ary = [];
try {
p(`obj = ${obj}`);
} catch (e) {
p(e);
}
p(`ary = ${ary}`);
p(`typeof obj: ${typeof obj}`);
p(`Array.isArray(obj): ${Array.isArray(obj)}`);
p(`typeof ary: ${typeof ary}`);
p(`Array.isArray(ary): ${Array.isArray(ary)}`);
try {
let c = obj + [];
} catch (e) {
p(e);
}
};
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';
output();
0 コメント:
コメントを投稿