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

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Baseline

20163

JS

2

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

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

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

let const

{}

"use strict"

3

  • :
  • :
  • :

16

static

:

constructor 2

constructor "constructor" 1 constructor SyntaxError

super

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

js
class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Getter
  get area() {
    return this.calcArea();
  }
  // Method
  calcArea() {
    return this.height * this.width;
  }
  *getSides() {
    yield this.height;
    yield this.width;
    yield this.height;
    yield this.width;
  }
}

const square = new Rectangle(10, 10);

console.log(square.area); // 100
console.log([...square.getSides()]); // [10, 10, 10, 10]

static

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

  static displayName = "Point";
  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); // "Point"
console.log(Point.distance(p1, p2)); // 7.0710678118654755

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

const JavaScript public private

undefined

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

#

extends

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

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

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

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

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

this 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}`);
  }
}

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

class class

  1. extends null TypeError
  2. constructor constructor constructor
  3. this this
  4. prototype
  5. extends constructor ReferenceError
    • super()
    • this
    • this

constructor

this this undefined "use strict" class

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

const obj = new Animal();
obj.speak(); // the Animal object
const speak = obj.speak;
speak(); // undefined

Animal.eat(); // class Animal
const eat = Animal.eat;
eat(); // undefined

this globalThis this undefined

js
function Animal() {}

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

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

const obj = new Animal();
const speak = obj.speak;
speak(); // global object (in nonstrict mode)

const eat = Animal.eat;
eat(); // global object (in non-strict mode)

ECMAScript 2027 LanguageSpecification
# sec-class-definitions


Web Proxy Viewer  |  New URL  |  Original Page