Skip to content

Instantly share code, notes, and snippets.

@nakamura-to
Created January 13, 2012 03:43
Show Gist options
  • Save nakamura-to/1604587 to your computer and use it in GitHub Desktop.
Save nakamura-to/1604587 to your computer and use it in GitHub Desktop.
prototype chain
var assert = require('assert');
// Object -> Function.prototype -> Object.prototype -> null
assert.strictEqual(Object.__proto__, Function.prototype);
assert.strictEqual(Function.prototype.__proto__, Object.prototype);
assert.strictEqual(Object.prototype.__proto__, null);
// Function -> Function.prototype -> Object.prototype -> null
assert.strictEqual(Function.__proto__, Function.prototype);
assert.strictEqual(Function.prototype.__proto__, Object.prototype);
assert.strictEqual(Object.prototype.__proto__, null);
// o -> Object.prototype -> null
var o = {};
assert.strictEqual(o.__proto__, Object.prototype);
assert.strictEqual(Object.prototype.__proto__, null);
// Hoge -> Function.prototype -> Object.prototype -> null
var Hoge = function () {};
assert.strictEqual(Hoge.__proto__, Function.prototype);
assert.strictEqual(Function.prototype.__proto__, Object.prototype);
assert.strictEqual(Object.prototype.__proto__, null);
// hoge -> Hoge.prototype -> Object.prototype -> null
var hoge = new Hoge();
assert.strictEqual(hoge.__proto__, Hoge.prototype);
assert.strictEqual(Hoge.prototype.__proto__, Object.prototype);
assert.strictEqual(Object.prototype.__proto__, null);
// foo -> Foo.prototype (hoge) -> Hoge.prototype -> Object.prototype -> null
var Foo = function () {};
Foo.prototype = hoge;
var foo = new Foo();
assert.strictEqual(foo.__proto__, Foo.prototype);
assert.strictEqual(Foo.prototype, hoge);
assert.strictEqual(hoge.__proto__, Hoge.prototype);
assert.strictEqual(Hoge.prototype.__proto__, Object.prototype);
assert.strictEqual(Object.prototype.__proto__, null);
// bar -> Foo.prototype (hoge) -> Hoge.prototype -> Object.prototype -> null
var Bar = function () {};
var bar = new Bar();
bar.__proto__ = Foo.prototype;
assert.strictEqual(bar.__proto__, Foo.prototype);
assert.strictEqual(Foo.prototype, hoge);
assert.strictEqual(hoge.__proto__, Hoge.prototype);
assert.strictEqual(Hoge.prototype.__proto__, Object.prototype);
assert.strictEqual(Object.prototype.__proto__, null);
// baz -> Baz.prototype -> Foo.prototype (hoge) -> Hoge.prototype -> Object.prototype -> null
var Baz = function () {};
Baz.prototype.__proto__ = Foo.prototype;
var baz = new Baz();
assert.strictEqual(baz.__proto__, Baz.prototype);
assert.strictEqual(Baz.prototype.__proto__, Foo.prototype);
assert.strictEqual(Foo.prototype, hoge);
assert.strictEqual(hoge.__proto__, Hoge.prototype);
assert.strictEqual(Hoge.prototype.__proto__, Object.prototype);
assert.strictEqual(Object.prototype.__proto__, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment