Classes and Methods
Static Methods
Call a method on the class itself.
static-method
A static method belongs to the class, not to one instance. Use it for helpers that do not need instance fields.
Static Methods
static_method.js
Replay: real traced execution (multi-file project)
const amount = 8;
class Pricing {
static addFee(value) {
return value + 2;
}
}
const total = Pricing.addFee(amount);
console.log("amount=" + amount);
console.log("total=" + total);
const amount = 12;
class Pricing {
static addFee(value) {
return value + 2;
}
}
const total = Pricing.addFee(amount);
console.log("amount=" + amount);
console.log("total=" + total);
const amount = 20;
class Pricing {
static addFee(value) {
return value + 2;
}
}
const total = Pricing.addFee(amount);
console.log("amount=" + amount);
console.log("total=" + total);
amount ← 8
1const amount→ 8 = 8; //@amount=12, 202class Pricing {3 static addFee(value) {4 return value + 2;5 }6}78const total = Pricing.addFee(amount8);static addFee(value)
1const amount = 8; //@amount=12, 202class Pricing {3 static addFee(value8) {4 return value + 2;5 }total ← 10
5 }6}78const total→ 10 = Pricing.addFee(amount8);910console.log("amount=" + amount8);11console.log("total=" + total10);outputamount=8 total=10
amount ← 12
1const amount→ 12 = 12;2class Pricing {3 static addFee(value) {4 return value + 2;5 }6}78const total = Pricing.addFee(amount12);static addFee(value)
1const amount = 12;2class Pricing {3 static addFee(value12) {4 return value + 2;5 }total ← 14
5 }6}78const total→ 14 = Pricing.addFee(amount12);910console.log("amount=" + amount12);11console.log("total=" + total14);outputamount=12 total=14
amount ← 20
1const amount→ 20 = 20;2class Pricing {3 static addFee(value) {4 return value + 2;5 }6}78const total = Pricing.addFee(amount20);static addFee(value)
1const amount = 20;2class Pricing {3 static addFee(value20) {4 return value + 2;5 }total ← 22
5 }6}78const total→ 22 = Pricing.addFee(amount20);910console.log("amount=" + amount20);11console.log("total=" + total22);outputamount=20 total=22