Created
October 20, 2013 05:37
-
-
Save h13i32maru/7065416 to your computer and use it in GitHub Desktop.
javascript constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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