aboutsummaryrefslogtreecommitdiff
path: root/files/sv-se/web/javascript/reference/classes/static/index.html
blob: 515f4fbfecb2512d6d1fbc5a1b10e6c76fe6b8e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
---
title: static
slug: Web/JavaScript/Reference/Classes/static
tags:
  - Klasser
  - Metoder
  - Statiska funktioner
translation_of: Web/JavaScript/Reference/Classes/static
---
<div>{{jsSidebar("Classes")}}</div>

<p>Nyckelordet <strong>static</strong> definierar en statisk metod för en klass.</p>

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

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

<h2 id="Beskrivning">Beskrivning</h2>

<p>Anrop på statiska metoder är gjorda direkt på klassen och kan inte göras genom instanser av klassen. Statiska metoder är ofta använda för att göra verktygsfunktioner.</p>

<h2 id="Att_anropa_statiska_metoder">Att anropa statiska metoder</h2>

<h3 id="Från_en_annan_statisk_metod">Från en annan statisk metod</h3>

<p>För att anropa en statisk metod från en annan statisk metod av samma klass, kan du använda "<code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/this">this</a>".</code></p>

<pre class="brush: js">class StaticMethodCall {
  static staticMethod() {
    return 'En statisk metod har blivit anropad';
  }
  static anotherStaticMethod() {
    return this.staticMethod() + ' från en annan statisk metod!';
  }
}
StaticMethodCall.staticMethod();
// 'En statisk metod har blivit anropad'

StaticMethodCall.anotherStaticMethod();
// 'En statisk metod har blivit anropad från en annan statisk metod!'</pre>

<h3 id="Från_en_klasskonstruktor_och_andra_metoder">Från en klasskonstruktor och andra metoder</h3>

<p>Statiska metoder är inte tillgängliga genom att använda "<code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/this">this</a>"</code> från icke statiska metoder. Du behöver anropa dem genom att antingen använda klassnamnet: ClassName.staticMethodName() eller genom att anropa metoden som en egendom av konstruktorn: this.constructor.staticMethodName().</p>

<pre class="brush: js">class StaticMethodCall {
  constructor() {
    console.log(StaticMethod.staticMethod());
    // 'En statisk metod har blivit anropad.'

    console.log(this.constructor.staticMethod());
    // 'En statisk metod har blivit anropad.'
  }

  static staticMethod() {
    return 'En statisk metod har blivit anropad.';
  }
}</pre>

<h2 id="Exempel">Exempel</h2>

<p>Det följande exemplet visar flera saker:</p>

<ol>
 <li>Hur en statisk metod implementeras på en klass.</li>
 <li>Att en klass med en statisk medlem kan vara sub-klassad.</li>
 <li>Hur en statisk metod kan och inte kan bli anropad.</li>
</ol>

<pre class="brush: js">class Triple {
  static triple(n) {
    if (n === undefined) {
      n = 1;
    }
    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

var tp = new Triple();

console.log(BiggerTriple.triple(3));
// 81 (Påverkas inte av förälderns instans.)

console.log(tp.triple());
// 'tp.triple is not a function'.
</pre>

<h2 id="Specifikationer">Specifikationer</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specifikation</th>
   <th scope="col">Status</th>
   <th scope="col">Kommentar</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-class-definitions', 'Class definitions')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Första definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-class-definitions', 'Class definitions')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Webbläsarkompatibilitet">Webbläsarkompatibilitet</h2>

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

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Funktion</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Grundlig 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>Funktion</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>Grundlig 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="sect1"> </h2>

<h2 id="Läs_också">Läs också</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>