자바스크립트의 핵심 기능이자 실제적인 체계는 전적으로 객체를 다른 객체와 연결하는 것에서 비롯된다.
Task = {
setID: function(ID) { this.id = ID; },
outputID: function() { console.log(this.id); },
};
XYZ = Object.create(Task);
XYZ.prepareTask = function(ID, Label) {
this.setID(ID);
this.label = Label;
};
XYZ.outputTaskDetails = function() {
this.outputID();
console.log(this.label);
}
ABC = Object.create(Task);
// ABC = ...
상호위임(허용되지 않음) : 복수의 객체가 서로 위임하고 있는 상태 -> 에러발생
function Foo(who) {
this.me = who;
}
Foo.prototype.identify = function () {
return "I am " + this.me;
};
function Bar(who) {
Foo.call(this, who);
}
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.speak = function() {
alert("Hello, ", this.identify() + ".");
};
var b1 = new Bar("b1");
var b2 = new Bar("b2");
b1.speak();
b2.speak();
Foo = {
init: function(who) {
this.me = who;
},
identify: function() {
return "I am " + this.me;
},
};
Bar = Object.create(Foo);
Bar.speak = function() {
alert("Hello, " + this.identify() + ".");
};
var b1 = Object.create(Bar);
b1.init("b1");
var b2 = Object.create(Bar);
b2.init("b2");
b1.speak();
b2.speak();