Scenario Overview
In a restaurant, customers can order multiple dishes, and we want to calculate their total bill based on the prices of the dishes ordered, any applicable taxes, and discounts. We’ll create a function to compute the total bill and use call, apply, and bind to handle different customers and their orders.
Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | function calculateTotalBill(taxRate, discount) {
const taxAmount = this.orderTotal * (taxRate / 100);
const totalBill = this.orderTotal + taxAmount - discount;
return totalBill;
}
const customer1 = {
name: "Sarah" ,
orderTotal: 120
};
const customer2 = {
name: "Mike" ,
orderTotal: 200
};
const totalBillSarah = calculateTotalBill.call(customer1, 10, 15);
console.log(`${customer1.name} 's Total Bill: $${totalBillSarah}`); // Output: Sarah' s Total Bill: $117
const taxRateMike = 8;
const discountMike = 20;
const totalBillMike = calculateTotalBill.apply(customer2, [taxRateMike, discountMike]);
console.log(`${customer2.name} 's Total Bill: $${totalBillMike}`); // Output: Mike' s Total Bill: $176
const calculateSarahBill = calculateTotalBill.bind(customer1);
const totalBillSarahAfterDiscount = calculateSarahBill(5, 10);
console.log(`${customer1.name} 's Total Bill after New Discount: $${totalBillSarahAfterDiscount}`); // Output: Sarah' s Total Bill after New Discount: $115
|
Nach dem Login kopieren
Explanation
-
Function Definition:
- We define a function calculateTotalBill that takes taxRate and discount as parameters. This function calculates the total bill by adding tax to the order total and subtracting any discounts.
-
Customer Objects:
- We create two customer objects, customer1 (Sarah) and customer2 (Mike), each with their respective names and total order amounts.
-
Using call:
- For Sarah, we calculate her total bill using the call method. This allows us to specify customer1 as the context and pass the tax rate and discount as separate arguments. The output shows Sarah's total bill after applying the tax and discount.
-
Using apply:
- For Mike, we use the apply method to calculate his total bill. This method allows us to pass the parameters as an array, making it convenient for handling multiple arguments. The total bill for Mike is calculated similarly but with different tax and discount values.
-
Using bind:
- We create a bound function for Sarah using bind, which locks the context to customer1. This means we can later call this new function without needing to specify this again. We demonstrate this by calculating Sarah's total bill again with different parameters, allowing for flexible future calculations.
Output
- The console logs display the total bills for both Sarah and Mike based on their respective orders and discounts.
- This example effectively demonstrates how call, apply, and bind can be used to manage function contexts within a restaurant billing system.
Conclusion
This scenario highlights how call, apply, and bind can be utilized in a practical setting, such as calculating restaurant bills, helping to understand how these methods facilitate the management of this in JavaScript.
Das obige ist der detaillierte Inhalt vonVerwendung von „call', „apply' und „bind' in einem Restaurant-Abrechnungssystem.. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!