Skip to content

Instantly share code, notes, and snippets.

@teramako
Created July 4, 2013 17:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teramako/5929337 to your computer and use it in GitHub Desktop.
Save teramako/5929337 to your computer and use it in GitHub Desktop.
__proto__ のテスト
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>__proto__ test</title>
</head>
<body>
<h1>__proto__ test</h1>
<div id="out"></div>
<script>
var $out = document.getElementById("out");
function print(msg){
out.insertAdjacentHTML("BeforeEnd", msg);
}
// 1
var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");
var props = Object.getOwnPropertyNames(desc);
print(
"<h2>Descriptor</h2>" +
"<dl>" +
props.map(function (key) {
return "<dt>" + key + "</dt><dd>" + desc[key] + "</dd>"
}).join("") +
"</dl>"
);
var proto = {
getName: function (){ return "PROTO"; }
};
var obj = {
__proto__: proto
};
print("<h2>var obj = { __proto__: proto }</h2>");
print("<p>obj.__proto__ is proto: " + (Object.getPrototypeOf(obj) === proto) + "</p>");
var obj2 = Object.create(null);
print("<h2>var obj2 = Object.create(null)<h2>");
print("<p>__proto__ in obj2: " + ("__proto__" in obj2) + "</p>");
obj2.__proto__ = "OK";
print("<p>obj2 hasOwn '__proto__': " + Object.prototype.hasOwnProperty.call(obj2, "__proto__") + "</p>");
print("<p>JSON.stringify(obj2): " + JSON.stringify(obj2) + "</p>");
var obj3 = JSON.parse('{ "__proto__": "PROTO" }');
print("<h2>var obj3 = JSON.parse('{ \"__proto__\": \"PROTO\" }')</h2>");
print("<p>obj3's keys: " + Object.keys(obj3) + "</p>");
print("<p>JSON.stringify(obj3): " + JSON.stringify(obj3) + "</p>");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment