class

Esta tradução está incompleta. Ajude a traduzir este artigo em inglês

A declaração class cria uma nova classe com dado nome usando a herança do protótipo base.

 

Você também pode definir uma classe usando class expression.

Sintaxe

class name [extends] {
  // class body
}

Descrição

Assim como as expressões de classe, o corpo de uma declaração de classe é executado em strict mode.

Declarações de classe não são hoisted (ao contrário de function declarations).

Exemplos

Declaração simples de classe

No exemplo a seguir, primeiro definimos uma classe chamada Polygon, então extendemos essa classe para criar uma classe chamada Square. Note que super(), usada no construtor, só pode ser usada em construtores e deve ser chamada antes da chave this ser usada.

class Poligono {
  constructor(altura, largura) {
    this.nome = 'Polígono';
    this.altura = altura;
    this.largura = largura;
  }
}

class Quadrado extends Poligono {
  constructor(altura) {
    super(altura, altura);
    this.nome = 'Quadrado';
  }
}

Especificações

Especificação Status Comentário
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Class definitions' in that specification.
Padrão definição inicial.

Compatibilidade de Browser

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
classChrome Full support 49
Full support 49
No support 42 — 49
Notes
Notes Strict mode is required.
No support 42 — 49
Disabled
Disabled From version 42 until version 49 (exclusive): this feature is behind the Experimental JavaScript preference (needs to be set to Enabled). To change preferences in Chrome, visit chrome://flags.
Edge Full support 13Firefox Full support 45IE No support NoOpera Full support 36
Full support 36
No support 29 — 36
Notes
Notes Strict mode is required.
No support 29 — 36
Disabled
Disabled From version 29 until version 36 (exclusive): this feature is behind the Experimental JavaScript preference (needs to be set to Enabled).
Safari Full support 10.1WebView Android Full support 49
Full support 49
No support 42 — 49
Notes
Notes Strict mode is required.
Chrome Android Full support 49
Full support 49
No support 42 — 49
Notes
Notes Strict mode is required.
No support 42 — 49
Disabled
Disabled From version 42 until version 49 (exclusive): this feature is behind the Experimental JavaScript preference (needs to be set to Enabled). To change preferences in Chrome, visit chrome://flags.
Firefox Android Full support 45Opera Android Full support 36
Full support 36
No support 29 — 36
Notes
Notes Strict mode is required.
No support 29 — 36
Disabled
Disabled From version 29 until version 36 (exclusive): this feature is behind the Experimental JavaScript preference (needs to be set to Enabled).
Safari iOS Full support 10.3Samsung Internet Android Full support 5.0
Full support 5.0
No support 4.0 — 5.0
Notes
Notes Strict mode is required.
nodejs Full support 6.0.0

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

Veja também