functions and methods
JS Function vs Method
Function
// Function example
function calculateTotal(items) {
// ...
}
const calculateTotal = function(items) {
// ...
};// Function example
function calculateTotal(items) {
// ...
}
const calculateTotal = function(items) {
// ...
};// Method example
const cart = {
items: [],
addItem(item) {
this.items.push(item); // `this` refers to `cart`
}
};
class Cart {
constructor() {
this.items = [];
}
addItem(item) {
this.items.push(item); // `this` refers to the instance
}
}