Skip to content

Instantly share code, notes, and snippets.

@h13i32maru
Created October 20, 2013 05:37
Show Gist options
  • Save h13i32maru/7065416 to your computer and use it in GitHub Desktop.
Save h13i32maru/7065416 to your computer and use it in GitHub Desktop.
javascript constructor
function A(){};
function B(){};
function C(){};
B.prototype = new A();
// Cのprototypeを変更しない場合は正常.
console.log(C.prototype.constructor); // C
console.log((new C()).constructor); // C
// CのprototypeをBのインスタンスで置き換得てしまうだけではおかしなことになる.
C.prototype = new B();
console.log(C.prototype.constructor); // A
console.log((new C()).constructor); // A
// Cのprototypeを変更したらconstructorも正しいものにしておく必要がある.
C.prototype.constructor = C;
console.log((new C()).constructor); // C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment