LoginSignup
1
0

JavaScript: 豆知識

Last updated at Posted at 2024-06-09

暗黙型変換

果たしてMath.maxは何と答えるのか!? Number? NaN? それどころか文法違反? 1文字も見落とさずじっくり観察してね。

Math.max(!1,!0)
Math.max(-0,null)
Math.max(1,0..a)
Math.max(1,NaN)
Math.max(-1,true)
Math.max(-1,"")
Math.max(-1,[])
Math.max(-1,[,])
Math.max(-1,[,1])
Math.max(-011,[-1e-1-1])
Math.max(-1,[{}])
Math.max(011,10)
Math.max("011",10)
Math.max(-["-012"],013)
Math.max(" \n011\n ",012)
Math.max(" \11\n","\r\f\v")
Math.max("0b11","2")
Math.max(1,-"-2")
Math.max(1,-"-0x2")
Math.max("1e2",111)
Math.max("-1e-1",-.1)
Math.max(-"-1e-1",-.1)
Math.max("Infinity",1)
Math.max(-"-Infinity",1)
Math.max(" \rInfinity\n ",1)
Math.max("1e+309",1e308)
Math.max(1,parseInt(0.0000009))
Math.max(-1,`${`${`''`}`}`)
Math.max(1e308,1/1e-309)
Math.max(1e-323,1e-324)
Math.max("1/0",1)
Math.max("1n",2n)
Math.max(1<<30,-1>>>1)
Math.max(-~.3e+10,-.3e+10)
Math.max(-~-~0-~-~0,~-~-~-~-0)
Math.max(/0/,1)
Math.max([+!+[]]+[],[!+[]+!+[]])
Math.max(void-1,void 0)
Math.max(new Number,new Int8Array(1))
Math.max(String(),RegExp())

何だよ、こんなの楽勝過ぎるぜ! ごもっともです、はい。

RegExp

以下の処理でa~jの値を答えよ

a=RegExp().test(0)
b=/\//i/ /\//i.test("//i/ //")
c=/\//i//\//i.test("//i///")
d=/$/.test()
e=/^|$|/.test("")
f=/Number/.test(0..constructor)
g=/10/.test(012)
h=/g//g
i=/g///g/
j=/g// /g/

String

a~cの値は何ぞ? cの値は、空白類には何種類の文字があるのか知らないと解けません

a=String().replace(RegExp(),Math.exp);
b=String.fromCharCode(066,66,"066",eval("066"));
for(c=0,z=65536;z;)/\s/.test(String.fromCharCode(--z))&&c++;

何やら奇妙な結果に…

let a,b=0,c=0,t="\uD83D\uDC69\u200D\uD83D\uDC69\u200D\uD83D\uDC66\u200D\uD83D\uDC66";
for(a of t)b++;
for(a in t)c++;
console.log(b,c)

Array

a~bの値を答えたまえ

a=[1];a[4294967295]=1;
a=a.lastIndexOf(1);
b=[0/0].indexOf(0/0);

Function

以下の処理では何が出力されるか? 文法違反じゃないかって? さあ、それはどうでしょう…。これは摩訶不思議なJavaScriptの物語なのです

function a(a,a){console.log(a)}
a(1,2)

以下のinput要素をclickした時、文法違反となるか、何も起こらないか、あるいはconsole.logで何が出力されるか、各input要素ごとに答えよ。

<input type=button value=this onclick=console.log (value==this)>
<input type=button value=URL onclick=try{Function("[a,b=URL",..."]URL","console.log(a==b,arguments[3]==URL,URL==this.URL)")([URL,,URL],URL)}catch(a){console.log(self.URL==URL,value==URL)} URL=URL>
<input type=button value=Math onclick=try{Function("[a,b=Math",..."]Math","console.log(a===b,arguments[2]===Math,Math.max('\\40','\\12'))")([Math,,Math])}catch(a){console.log(Math.max("011",012))}>

上記問題には幾つもの巧妙な罠が仕掛けられています。
まずonclickの属性値からして不備はないか? 不備があれば何も起こりません。
例えば引用符類で囲ってないけど、中身はHTML的に正しい文字列のみで構成されているか?
そこを正しく理解した上で、お次はJavaScriptとして文法違反はないか読み解く。
そのためにはまずFunction()の仕様を理解しておく必要があります。
第一引数に指定できるものは? 同じく第二引数は? それ以降の引数は?

その他

IE6辺りでは動作不能な変態program

if(0);else a:do while(1)if(1)break a;else;while(1){a:0}

何が出力されるか?

let of=[,0,1];
for(of of of)console.log(of);
console.log(of)

前提

ECMA SCRIPT 6以降として解釈して下さい。ついでに"use strict"の記述はしない

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0