[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/class [Back]  [Original]

class expression - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

class expression

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2016 ..

Class expression ECMAScript 2015 (ES6). function expressions, class expressions . , . JavaScript - .

In this article

var MyClass = class [className] [extends] {
  //  
};

Class expression class declaration (statement). class expression ("binding identifier"), class declaration. class expression , class declaration. . typeof , class expression, "function".

, class declaration, class expression .

js
"use strict";
var Foo = class {}; //   
var Foo = class {}; //   

typeof Foo; //  "function"
typeof class {}; //  "function"

Foo instanceof Object; // true
Foo instanceof Function; // true
class Foo {} // Throws TypeError, doesn't allow re-declaration

class expression

class expression, "Foo".

js
var Foo = class {
  constructor() {}
  bar() {
    return "Hello World!";
  }
};

var instance = new Foo();
instance.bar(); // "Hello World!"
Foo.name; // "Foo"

class expression

, class expression. class expression.

js
var Foo = class NamedFoo {
  constructor() {}
  whoIsThere() {
    return NamedFoo.name;
  }
};
var bar = new Foo();
bar.whoIsThere(); // "NamedFoo"
NamedFoo.name; // ReferenceError: NamedFoo is not defined
Foo.name; // "NamedFoo"

Specification
ECMAScript 2027 LanguageSpecification
# sec-class-definitions


Web Proxy Viewer  |  New URL  |  Original Page