Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

static

翻譯不完整。請協助 翻譯此英文文件

關鍵字 static 定義一個靜態的方法(method)給類別(class).
 

語法

static methodName() { ... }

敘述

Static methods are called without instantiating their class and are also not callable when the class is instantiated. Static methods are often used to create utility functions for an application.

 

範例

下面的範例示範了許多東西。他示範了如何在一個可以有子類別的類別中實作一個靜態方法。最後示範了靜態方法在什麼情形下正確與錯誤的呼叫。

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'.

規格

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Class definitions' in that specification.
Standard Initial definition.
ECMAScript 2016 Draft (7th Edition, ECMA-262)
The definition of 'Class definitions' in that specification.
Draft  

瀏覽器相容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 42.0 45 (45) ? ? ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support Not supported 45.0 (45) ? ? ? 42.0

你可能會需要看看

文件標籤與貢獻者

 此頁面的貢獻者: Robert-Rino
 最近更新: Robert-Rino,