-
-
Save tkihira/808ca736e6ff289844c65bbfd3ce5fe5 to your computer and use it in GitHub Desktop.
How to make `null` value without using `null` literal
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
// pattern 1: eval or parse | |
console.log(eval("null")); | |
console.log(JSON.parse("null")); | |
console.log(JSON.parse(JSON.stringify([undefined]))[0]); | |
// pattern 2: Date.prototype.toJSON | |
console.log(new Date(NaN).toJSON()); | |
// pattern 3: RegExp related | |
console.log(/a/.exec("")); | |
console.log("".match(/a/)); | |
// pattern 4: retrieve prototype of Object.prototype (by azu san) | |
console.log(Object.prototype.__proto__); | |
console.log(Object.getPrototypeOf(Object.prototype)); | |
console.log(Reflect.getPrototypeOf(Object.prototype)); // by Yosuke HASEGAWA san | |
console.log(Object.__proto__.__proto__.__proto__); // by Yosuke HASEGAWA san | |
(function foo(){ console.log(foo.caller)})(); // by Yosuke HASEGAWA san |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
document.all
をECMA262に含めてよいのであれば、これでもnull値を作れるようです。ref. https://github.com/tc39/test262/blob/b5d3192914e9b107ce4f5df4f77e625865d337c7/test/annexB/built-ins/String/prototype/replaceAll/custom-replacer-emulates-undefined.js#L19-L29
(追記)このTest262のテストケースは
If replacer is not undefined, then
を確認するためのもので、document.all()
がnullを返すことはECMA262の仕様の範囲外なので、本問題の回答としては不適切かもしれません。お騒がせして申し訳ありませんでした。