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

Revision 1069178 of static

  • Revision slug: Web/JavaScript/Reference/Classes/static
  • Revision title: static
  • Revision id: 1069178
  • Created:
  • Creator: paulwithap
  • Is current revision? No
  • Comment Fix typo in example + fix grammar

Revision Content

{{jsSidebar("Classes")}}

The static keyword defines a static method for a class.

Syntax

static methodName() { ... }

Description

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.

Calling static method

From another static method

In order to call another static method in static method you can simply use this keyword.

class StaticMethodCall {
    static staticMethod() {
        return 'Static method has been called';
    }
    static anotherStaticMethod() {
        return this.staticMethod() + ' from another static method';
    }
}
StaticMethodCall.staticMethod(); // 'Static method has been called'
StaticMethodCall.anotherStaticMethod(); // 'Static method has been called from another static method'

 

From class constructor and other methods

As static method is part of class, it's not accessible through this keyword. the solution to the problem would be using of CLASSNAME.STATIC_METHOD_NAME (like other calls from outside of class) or this.constructor.STATIC_METHOD_NAME.

class StaticMethodCall{
    constructor(){
        console.log(StaticMethodCall.staticMethod()); // 'static method has been called' 
        console.log(this.constructor.staticMethod()); // 'static method has been called' 
    }

    static  staticMethod(){
        return 'static method has been called.';
    }
}

Examples

The following example demonstrates several things. It shows how a static method is implemented on a class and that a class with a static member can be subclassed. Finally it shows how a static method can and cannot be called.

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(BiggerTriple.triple(3)); // 81 (not affected by parent's instantiation)
console.log(tp.triple());            // 'tp.triple is not a function'.

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-class-definitions', 'Class definitions')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-class-definitions', 'Class definitions')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(42.0)}} {{CompatGeckoDesktop(45)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatGeckoMobile(45)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(42.0)}}

See also

Revision Source

<div>{{jsSidebar("Classes")}}</div>

<p>The <strong>static</strong> keyword defines a static method for a class.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
static methodName() { ... }</pre>

<h2 id="Description">Description</h2>

<p>Static methods are called without instantiating their class and&nbsp;are also not&nbsp;callable when the class is instantiated. Static methods are often used to create utility functions for an application.</p>

<h2 id="Calling_static_method">Calling static method</h2>

<h3 id="From_another_static_method">From another static method</h3>

<p>In order to call another static method in static method you can simply use <a href="/en-US/docs/Web/JavaScript/Reference/Operators/this">this</a> keyword.</p>

<pre class="brush: js">
class StaticMethodCall {
    static staticMethod() {
        return 'Static method has been called';
    }
    static anotherStaticMethod() {
        return this.staticMethod() + ' from another static method';
    }
}
StaticMethodCall.staticMethod(); // 'Static method has been called'
StaticMethodCall.anotherStaticMethod(); // 'Static method has been called from another static method'</pre>

<h3 id="sect1">&nbsp;</h3>

<h3 id="From_class_constructor_and_other_methods">From class constructor and other methods</h3>

<p>As static method is part of class, it's not accessible through this keyword. the solution to the problem would be using of <code>CLASSNAME.STATIC_METHOD_NAME</code> (like other calls from outside of class) or <code>this.constructor.STATIC_METHOD_NAME</code>.</p>

<pre class="brush: js">
class StaticMethodCall{
    constructor(){
        console.log(StaticMethodCall.staticMethod()); // 'static method has been called' 
        console.log(this.constructor.staticMethod()); // 'static method has been called' 
    }

    static  staticMethod(){
        return 'static method has been called.';
    }
}</pre>

<h2 id="Examples">Examples</h2>

<p>The following example demonstrates several things. It shows how a static method is implemented on a class and that a class with a static member can be subclassed. Finally it shows how a static method can and cannot be called.</p>

<pre class="brush: js">
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(BiggerTriple.triple(3)); // 81 (not affected by parent's instantiation)
console.log(tp.triple());            // 'tp.triple is not a function'.</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-class-definitions', 'Class definitions')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-class-definitions', 'Class definitions')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(42.0)}}</td>
   <td>{{CompatGeckoDesktop(45)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(45)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(42.0)}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/class"><code>class</code> expression</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/class"><code>class</code> declaration</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Classes">Classes</a></li>
</ul>
Revert to this revision