mozilla
Your Search Results

    Classes

    This translation is incomplete. Please help translate this article from English.

    Это экспериментальная технология, часть предложения Harmony (ECMAScript 6).
    Поскольку спецификация этой технологии ещё не стабилизировалась, проверьте таблицу совместимости её использования в различных браузерах. Также обратите внимание, что синтаксис и поведение экспериментальной технологии могут быть изменены в будущих версиях браузеров в соответствии с изменениями в спецификации.

    Draft
    This page is not complete.

    JavaScript classes are introduced in ECMAScript 6 and are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JS classes provide a much simpler and clearer syntax to create objects and dealing with inheritance.

    Defining classes

    Classes are in fact functions, and just like you can define function expressions and function declarations, the class syntax has the two opponents:

    Class declarations

    One way to define a class is a class declaration. For that, you need a class keyword and a name of the class ("Polygon" here).

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

    Hoisting

    A difference between function declarations and class declarations is that function declarations are hoisted and class decorations are not. You first need to declare your class and then access it, otherwise code like the following will throw a ReferenceError:

    var p = new Polygon(); // ReferenceError
    
    class Polygon {}
    

    Class expressions

    A class expression is another way to define a class and it can be named or unnamed. If named, the name of the class is local the class body only.

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

    Class bodies and method definitions

    The body of a class is the part that is in curly brackets ({}).This is were you define class members, such as methods or constructors.

    Strict mode

    The body of class declarations and class expressions is executed in strict mode.

    Constructor

     

    Static methods

     

    Sub classing with extends

     

    Sub classing built-in objects

     

    Super class calls with super

     

    Examples

     

    Specifications

    Specification Status Comment
    ECMAScript 6 (ECMA-262)
    Определение 'Class definitions' в этой спецификации.
    Кандидат в рекомендации 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 Нет Available in the Nightly channel only (since February 2015) ? ? ? 42.0

    See also

    Метки документа и участники

    Contributors to this page: fscholz
    Обновлялась последний раз: fscholz,
    Скрыть боковую панель