Skip to content

Instantly share code, notes, and snippets.

@elarkin
Created December 16, 2010 04:47
Show Gist options
  • Save elarkin/743045 to your computer and use it in GitHub Desktop.
Save elarkin/743045 to your computer and use it in GitHub Desktop.
.__proto__.constructor vs .constructor
var A = function A() {};
var B = function B() {};
B.prototype = new A();
// B.prototype.constructor = B; // only way I can find to get bi's constructor to be B. Comment or uncomment this line to produce exactly the same output for both tests. It doesn't matter.
// require('util').inherits(B, A); // same here. Comment or uncomment it doesn't make a difference.
var ai = new A();
var bi = new B();
console.log('Test 1: constructor directly');
console.log(ai.constructor.name);
console.log(bi.constructor.name);
console.log('');
console.log('Test 2: constructor through __proto__');
console.log(ai.__proto__.constructor.name);
console.log(bi.__proto__.constructor.name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment