__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)";
}
  1. IE のみ Object.prototype に "__proto__" プロパティがない
    • ECMAScript 5th 実装である Object.getPrototypeOf があれば IE9 以上と判定
  2. JSON.parse('{"__proto__":null}') の挙動をみる
    1. ObjectのインスタンスでなくなってしまうのがV8エンジン(GoogleChrome)
    2. Objectのインスタンスであると判定されながら、Object.getPrototypeOf をすると null が返るのが JavaScriptCore(Safari)
  3. Firefox(SpiderMonkey) は Object.prototype に "__proto__" プロパティを持つ
  4. それ以外は Carakan(Opera)

テストしたブラウザ

2013-03-03(コード修正)