--- title: static slug: Web/JavaScript/Reference/Classes/static translation_of: Web/JavaScript/Reference/Classes/static ---
static methodName() { ... }
靜態方法不須實例化(instantiating)其類別即可被呼叫,但當類別被實例化(instantiating)後則靜態方法不能被呼叫。 靜態方法常被使用在建立應用程式的工具函式(utility functions)。
下面的範例示範了許多東西。他示範了如何在一個可以有子類別的類別中實作一個靜態方法。最後示範了靜態方法在什麼情形下正確與錯誤的呼叫。
class Triple { static triple(n) { n = n || 1; //should not be a bitwise operation return n * 3; } } class BiggerTriple extends Triple { static triple(n) { return super.triple(n) * super.triple(n); } } console.log(Triple.triple()); // 3 console.log(Triple.triple(6)); // 18 console.log(BiggerTriple.triple(3)); // 81 var tp = new Triple(); console.log(tp.triple()); // 'tp.triple is not a function'.