Skip to content

Instantly share code, notes, and snippets.

@crongro
Created February 1, 2015 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crongro/a0ab19d23cb037abe301 to your computer and use it in GitHub Desktop.
Save crongro/a0ab19d23cb037abe301 to your computer and use it in GitHub Desktop.
JavaScript Inheritance (light version)
//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