The Wayback Machine - https://web.archive.org/web/20150905202331/https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Klassen

Klassen

von 1 Mitwirkenden:

Dieser Artikel benötigt eine redaktionelle Überprüfung.

Diese Übersetzung ist unvollständig. Bitte helfen Sie, diesen Artikel aus dem Englischen zu übersetzen.

This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

Draft
This page is not complete.

JavaScript Klassen, eingeführt in ECMAScript 6, sind syntaktischer Zucker für das bestehende, auf Prototypen basierende, Vererbungsmodell von JavaScript. Diese Syntaxerweiterung führt kein neues OOP-Modell in die Sprache ein. JS Klassen ermöglichen es, mit klarer und verständlicher Syntax Objekte zu erstellen und Vererbung in Javascript zu realisieren.
 

Klassendefinition

Klassen sind eigentlich Funktionen. Analog zu Funktionsausdrücken und Funktionsdeklarationen hat die Klassensyntax zwei Komponenten:

  • Klassenausdrücke und
  • Klassendeklaritionen.

Klassendeklaration

Eine Möglichkeit, Klassen zu definieren ist eine Klassendeklaration. Diese wird eingeleitet durch das Schlüsselwort class, gefolgt vom Namen der Klasse (hier: "Polygon"). 

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

Hoisting

Ein wichtiger Unterschied zwischen Klassen- und Funktionsdeklarationen besteht im sogenannten hoisted.  Funktionsdeklartionen werden an den Anfang des Definitionsbereichs "gehoben", für Klassen gilt dies nicht. Das heißt, um auf eine Klasse zuzugreifen, muss sie zuvor definiert worden sein, sonst führt dies zu einem  ReferenceError:

var p = new Polygon(); // ReferenceError

class Polygon {}

Klassenausdruck

Ein Klassenausdrück ist eine weitere Möglichkeit eine Klasse zu definieren. Dabei ist es optional, hinter dem Schlüsselwort class einen Namen anzugeben. Sollte ein Name angegeben werden, so gilt dieser nur innerhalb des Klassenrumpfs.

// unnamed
var Polygon = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};

// named
var Polygon = class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};

Klassenrumpf und Methodendefinitionen

Der Rumpf der Klasse ist innerhalb der beiden geschweiften Klammern {}. Hier werden die Eigenschaften der Klasse definiert, wie Konstruktoren oder Methoden. 

"Strict mode"

The bodies of class declarations and class expressions are executed in strict mode.

Constructor

The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name "constructor" in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method.

A constructor can use the super keyword to call the constructor of a parent class.

Prototype methods

See also method definitions.

// TBD

Static methods

The static keyword defines a static method for a class. Static methods are called without instantiating their class nor are they callable when the class is instantiated. Static methods are often used to create utility functions for an application.

// TBD 

Sub classing with extends

The extends keyword is used in class declarations or class expressions to create a class with a child of another class.

// TBD

Sub classing built-in objects

TBD

Super class calls with super

The super keyword is used to call functions on an object's parent.

// TBD

ES5 inheritance syntax and ES6 classes syntax compared

TBD

Examples

TBD

Specifications

Specification Status Comment
ECMAScript 6 (ECMA-262)
Die Definition von 'Class definitions' in dieser Spezifikation.
Anwärter Empfehlung Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 42.0 Available in the Nightly channel only (since February 2015) ? ? ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support Nicht unterstützt Available in the Nightly channel only (since February 2015) ? ? ? 42.0

See also

Schlagwörter des Dokuments und Mitwirkende

Mitwirkende an dieser Seite: DandlingPointer
Zuletzt aktualisiert von: DandlingPointer,
Seitenleiste ausblenden