Created
February 1, 2015 06:00
-
-
Save crongro/a0ab19d23cb037abe301 to your computer and use it in GitHub Desktop.
JavaScript Inheritance (light version)
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
//thin 'Object.create()' | |
function JClass(parentProto) { | |
var fun = new Function(); | |
fun.prototype = parentProto; | |
return new fun(); | |
} | |
// Parent function | |
function ESS(name) { | |
this.comma = ","; | |
} | |
ESS.prototype.print = function(name) { | |
return "cl" + this.comma + "" + this.name; | |
} | |
// Child function | |
function Name() { | |
ESS.call(this); | |
} | |
/* | |
// 이 방법은 prototype chain이 형성되지는 않고, 부모의 것을 copy해온 것과 같다. (on의 proto 체인확인필요) | |
Name.prototype = ESS.prototype; | |
Name.prototype.constructor = Name; | |
var on = new Name(); | |
on.setName("nayeon"); | |
on.print(); | |
*/ | |
//Name.prototype = Object.create(ESS.prototype); | |
Name.prototype = JClass(ESS.prototype); | |
Name.prototype.constructor = Name; | |
// Child method | |
// TODO. make object copy method | |
Name.prototype.setName= function(name) { | |
this.name = name; | |
} | |
// start call. | |
var on = new Name(); | |
on.setName("gg"); | |
on.print(); | |
// confirm Prototype Chain . | |
// expect 'true' | |
console.log(!!on.__proto__.__proto__.print); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment