当前位置:编程学习 > JS >>

JS组合模式

[javascript]
function Macbook() { 
    this.cost = function () { 
        return 1000; 
    }; 

 
function Memory(macbook) { 
    this.cost = function () { 
        return macbook.cost() + 75; 
    }; 

 
function BlurayDrive(macbook) { 
    this.cost = function () { 
        return macbook.cost() + 300; 
    }; 

 
 
function Insurance(macbook) { 
    this.cost = function () { 
        return macbook.cost() + 250; 
    }; 

 
 
// 用法 
var myMacbook = new Insurance(new BlurayDrive(new Memory(new Macbook()))); 
console.log(myMacbook.cost()); 



摘自 PainsOnline的专栏
补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,