__proto__の挙動からJavaScriptエンジン判定
function getJSengine() { if (!("__proto__" in Object.prototype)) { if (typeof Object.getPrototypeOf === "undefined") return "JScript(IE8-)"; else return "JScript(IE9+)" } var o = JSON.parse('{"__proto__":null}'); if (!(o instanceof Object)) return "V8(GoogleChrome26-)"; if (Object.getPrototypeOf(o) === null) return "JavaScriptCore(Safari)"; if (Object.prototype.hasOwnProperty("__proto__")) { var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"); if (desc.configurable) { if ("get" in desc) return "SpiderMonkey(Firefox17+)"; else return "SpiderMonkey(Firefox16-)"; } else { return "V8(GoogleChrome27+)"; } } else return "Carakan(Opera)"; }
- IE のみ Object.prototype に "__proto__" プロパティがない
- ECMAScript 5th 実装である Object.getPrototypeOf があれば IE9 以上と判定
JSON.parse('{"__proto__":null}')
の挙動をみる- ObjectのインスタンスでなくなってしまうのがV8エンジン(GoogleChrome)
- Objectのインスタンスであると判定されながら、Object.getPrototypeOf をすると null が返るのが JavaScriptCore(Safari)
- Firefox(SpiderMonkey) は Object.prototype に "__proto__" プロパティを持つ
- それ以外は Carakan(Opera)
テストしたブラウザ
- IE 8
- Firefox 18
- GoogleChrome 26.0.1403.0 dev-m
- Opera 12.50
- Safari 5.1.7
関連記事
2013-03-03(コード修正)
- V8 エンジンの挙動が変わった (参照: http://d.hatena.ne.jp/teramako/20130302/p1)
Object.prototype
に__proto__が定義されるようになった- SpiderMonkey と違い、descriptor の configurable が false である
- ついでに SpiderMonkey も Firefox 17 からは変わっている (参照: http://d.hatena.ne.jp/teramako/20120720/p1)
- getter/setter に変更されている