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

- 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

Baseline Widely available

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

JavaScript ECMAScript 2015 JavaScript . - , .

In this article

" ", , (function expressions function declarations), : class declarations class expressions.

class declaration ( ). class ( Rectangle).

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

(hoisting)

(function declaration) (class declaration) , (hoisting), . , ReferenceError:

js
var p = new Rectangle(); // ReferenceError

class Rectangle {}

class expression ( ). . , .

js
// 
var Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// : "Rectangle"

// 
var Rectangle = class Rectangle2 {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// : "Rectangle2"

: (hoisting), .

, {}. , .

(strict mode).

Constructor

constructor , , , . constructor. SyntaxError , constructor.

super constructor .

.

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

  get area() {
    return this.calcArea();
  }

  calcArea() {
    return this.height * this.width;
  }
}

const square = new Rectangle(10, 10);

console.log(square.area); // 100

static, . , (instance) . , , , , .

js
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  static displayName = "";
  static distance(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;

    return Math.hypot(dx, dy);
  }
}

const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
p1.displayName; //undefined
p1.distance; //undefined
p2.displayName; //undefined
p2.distance; //undefined

console.log(Point.displayName); // ""
console.log(Point.distance(p1, p2)); // 7.0710678118654755

this

this ( this boolean, string, number, undefined, null), this undefined . "use strict", .

js
class Animal {
  speak() {
    return this;
  }
  static eat() {
    return this;
  }
}

let obj = new Animal();
obj.speak(); //  Animal
let speak = obj.speak;
speak(); // undefined

Animal.eat(); //  Animal
let eat = Animal.eat;
eat(); // undefined

, this, . - this .

js
function Animal() {}

Animal.prototype.speak = function () {
  return this;
};

Animal.eat = function () {
  return this;
};

let obj = new Animal();
let speak = obj.speak;
speak(); //   ( )

let eat = Animal.eat;
eat(); //   ( )

:

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

(class-side) :

Rectangle.staticWidth = 20;
Rectangle.prototype.prototypeWidth = 25;

: - (stage 3), TC39 Javascript. , , Babel.

Javascript , :

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

, , .

.

, :

class Rectangle {
  #height = 0;
  #width;
  constructor(height, width) {
    this.#height = height;
    this.#width = width;
  }
}

. , , , , .

: .

, .

.

extends

extends , .

js
class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name}  .`);
  }
}

class Dog extends Animal {
  constructor(name) {
    super(name); //   super     name
  }

  speak() {
    console.log(`${this.name} .`);
  }
}

let d = new Dog("");
d.speak(); //  

, super, this.

, "":

js
function Animal(name) {
  this.name = name;
}
Animal.prototype.speak = function () {
  console.log(`${this.name}  .`);
};

class Dog extends Animal {
  speak() {
    console.log(`${this.name} .`);
  }
}

let d = new Dog("");
d.speak(); //  

//         .

, (non-constructible) . , Object.setPrototypeOf():

js
var Animal = {
  speak() {
    console.log(`${this.name}  .`);
  },
};

class Dog {
  constructor(name) {
    this.name = name;
  }
}

//     ,    TypeError   speak.
Object.setPrototypeOf(Dog.prototype, Animal);

let d = new Dog("");
d.speak(); //   .

Species

, Array MyArray. species .

, , map(), , , Array MyArray. Symbol.species :

js
class MyArray extends Array {
  //  species    Array
  static get [Symbol.species]() {
    return Array;
  }
}
var a = new MyArray(1, 2, 3);
var mapped = a.map((x) => x * x);

console.log(mapped instanceof MyArray); // false
console.log(mapped instanceof Array); // true

super

super .

js
class Cat {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name}  .`);
  }
}

class Lion extends Cat {
  speak() {
    super.speak();
    console.log(`${this.name} .`);
  }
}

let l = new Lion("");
l.speak();
//   .
//  .

Mix-ins

, mix-ins, . ECMAScript , ( , tooling classes) . .

mix-ins ECMAScript , , , :

js
var calculatorMixin = (Base) =>
  class extends Base {
    calc() {}
  };

var randomizerMixin = (Base) =>
  class extends Base {
    randomize() {}
  };

, mix-ins, :

js
class Foo {}
class Bar extends calculatorMixin(randomizerMixin(Foo)) {}

Specification
ECMAScript 2027 LanguageSpecification
# sec-class-definitions

. SyntaxError .

, Firefox Web Console (Tools > Web Developer > Web Console) ('Run') , SyntaxError: redeclaration of let ClassName;. ( Firefox bug 1428672.) Chrome Developer Tools Uncaught SyntaxError: Identifier 'ClassName' has already been declared at <anonymous>:1:1.


Web Proxy Viewer  |  New URL  |  Original Page