2017-10-20 53 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)而不是?