js.pattern-h 템플릿 방법 모드

1539 단어
-->

추상 클래스 만들기

var CaffeineBeverage = function () {

};
CaffeineBeverage.prototype.prepareRecipe = function () {
    this.boilWater();
    this.brew();
    this.pourOnCup();
    if (this.customerWantsCondiments()) {
        //  , 
 this.addCondiments();
    }
};
CaffeineBeverage.prototype.boilWater = function () {
    console.log(" !");
};
CaffeineBeverage.prototype.pourOnCup = function () {
    console.log(" !");
};
CaffeineBeverage.prototype.brew = function () {
    throw new Error(" !");
};
CaffeineBeverage.prototype.addCondiments = function () {
    throw new Error(" !");
};
//  
CaffeineBeverage.prototype.customerWantsCondiments = function () {
    return true;
};

추상류를 실현하다

//  
var Coffee = function () {
    CaffeineBeverage.apply(this);
};
Coffee.prototype = new CaffeineBeverage();
Coffee.prototype.brew = function () {
    console.log(" !");
};
Coffee.prototype.addCondiments = function () {
    console.log(" ");
};
Coffee.prototype.customerWantsCondiments = function () {
    return confirm(" ?");
};

// 
var Tea = function () {
    CaffeineBeverage.apply(this);
};
Tea.prototype = new CaffeineBeverage();
Tea.prototype.brew = function () {
    console.log(" !");
};
Tea.prototype.addCondiments = function () {
    console.log(" !");
};
Tea.prototype.customerWantsCondiments = function () {
    return confirm(" ?");
};

좋은 웹페이지 즐겨찾기