작동 위임

자바스크립트의 핵심 기능이자 실제적인 체계는 전적으로 객체를 다른 객체와 연결하는 것에서 비롯된다.

I. 위임 지향 디자인

1. 클래스 이론

2. 위임 이론

3. 멘탈 모델 비교

1) OOP 스타일

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();

2) OLOO 스타일

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();