2017-10-20 55 views
0

類我有下面的代碼片段,收集裏面的javascript

/* 
* The drink class, which defines the characteristics of the drink, in its simplest form, has its name 
*/ 
class Drink { 
    constructor(drinkName) { 
     this.Name = drinkName; 
    } 
} 

/* 
* The Customer class contains the properties of the customer along with the drinks preferred by the customer. 
*/ 
class Customer { 
    constructor(name, drinks) { 
     this.Name = name; 
     this.PreferredDrinks = drinks; 
     } 
    } 
function lazyBartender() { 

    var customers = []; 
    var c1 = new Customer('Cust1', [new Drink('n3'), new Drink('n7'), new Drink('n5'), new Drink('n2'), new Drink('n9')]); 
    customers.push(c1); 
    return customers; 
} 

當我把下面的語句,我得到的輸出

console.log(lazyBartender());

[ Customer { 
    Name: 'Cust1', 
    PreferredDrinks: [ [Object], [Object], [Object], [Object], [Object] ] } ] 
[Finished in 0.3s] 

相反如果我使用在lazyBartender()方法內的return c1;然後調用console.log,我得到如下輸出

Customer { 
    Name: 'Cust1', 
    PreferredDrinks: 
    [ Drink { Name: 'n3' }, 
    Drink { Name: 'n7' }, 
    Drink { Name: 'n5' }, 
    Drink { Name: 'n2' }, 
    Drink { Name: 'n9' } ] } 
[Finished in 0.3s] 

代碼有什麼問題,因爲如果我以這種方式實現,我在我的代碼中得到了一個stackoverflow異常。任何人都可以指出錯誤嗎?

完整的源代碼是如下,

/* 
* The drink class, which defines the characteristics of the drink, in its simplest form, has its name 
*/ 
class Drink { 
    constructor(drinkName) { 
     this.Name = drinkName; 
    } 
} 

/* 
* The Customer class contains the properties of the customer along with the drinks preferred by the customer. 
*/ 
class Customer { 
    constructor(name, drinks) { 
     this.Name = name; 
     this.PreferredDrinks = drinks; 
    } 
} 

class DrinkPref { 
    constructor(name, prefCount) { 
     this.Name = name; 
     this.Preference = prefCount || 0; 
    } 

    //Getter 
    get prefCt() { 
     return this.Preference; 
    } 

    increment() { 
     this.Preference += 1; 
    } 
} 

function doIncrement (collection, key) { 

    var existingItem = collection.find(function(value, index) { 
     return value.Name == key; 
    }); 

    if(existingItem == undefined) { 
     existingItem = new DrinkPref(key, 0); 
     collection.push(existingItem); 
    } 
    existingItem.increment(); 

} 

function getMax (collection, key) { 
    var maxValue = -Infinity; 

    collection.sort(function(a, b) { 
     return a.prefCt() > b.prefCt(); 
    }); 
} 

function getMinDrinksForAlCustomers(customers) { 
    var drinkChoices = []; 
    for (var i = 0; i < customers.length; i++) { 
     var customer = customers[i]; 

     for (var j = 0; j < customer.PreferredDrinks.length; j++) { 
      doIncrement(drinkChoices, customer.PreferredDrinks[j].Name); 
     } 
    } 

    getMax(drinkChoice); 

    return drinkChoices; 
} 

function lazyBartender() { 

    var c1 = new Customer('Cust1', [new Drink('n3'), new Drink('n7'), new Drink('n5'), new Drink('n2'), new Drink('n9')]); 
    var c2 = new Customer('Cust2', [new Drink('n5')]); 
    var c3 = new Customer('Cust3', [new Drink('n2'), new Drink('n3')]); 
    var c4 = new Customer('Cust4', [new Drink('n4')]); 
    var c5 = new Customer('Cust5', [new Drink('n3'), new Drink('n4'), new Drink('n3'), new Drink('n5'), new Drink('n7'), new Drink('n4')]); 
    var customers = [c1, c2,c3, c4, c5]; 

    return lazyBartender(customers); 
} 
console.log(lazyBartender()); 

獲取以下例外

RangeError: Maximum call stack size exceeded 
+1

我不能[轉載](http://jsbin.com/xucisifaro/edit?js,console)這個計算器。 –

+0

我已添加完整的源代碼和從崇高文本編輯器控制檯發生的異常。 – Saran

回答

0

只要改變lazyBartender到這 - 在return語句刪除遞歸調用:

function lazyBartender() { 

    var c1 = new Customer('Cust1', [new Drink('n3'), new Drink('n7'), new Drink('n5'), new Drink('n2'), new Drink('n9')]); 
    var c2 = new Customer('Cust2', [new Drink('n5')]); 
    var c3 = new Customer('Cust3', [new Drink('n2'), new Drink('n3')]); 
    var c4 = new Customer('Cust4', [new Drink('n4')]); 
    var c5 = new Customer('Cust5', [new Drink('n3'), new Drink('n4'), new Drink('n3'), new Drink('n5'), new Drink('n7'), new Drink('n4')]); 
    var customers = [c1, c2,c3, c4, c5]; 

    return customers; 
} 

我不知道你試圖用遞歸完成什麼調用(return lazyBartender(customers)) - lazyBartender不接受任何參數,它沒有任何基本情況,所以它會一直重複調用自己,給你錯誤。

也許你打算做return getMinDrinksForAlCustomers(customers)而不是?