| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Classes | [Back] [Original] |
Get to know MDN better
//
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;
}
};
{}
3
16
constructor "constructor" 1 constructor SyntaxError
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
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]
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
class Rectangle {
height = 0;
width;
constructor(height, width) {
this.height = height;
this.width = width;
}
}
const JavaScript public private
undefined
class Rectangle {
#height = 0;
#width;
constructor(height, width) {
this.#height = height;
this.#width = width;
}
}
#
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
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();
//
//
extends null TypeError constructor constructor constructor this this prototype extends constructor ReferenceError this this undefined "use strict" class
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
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 |
classclass | Web Proxy Viewer | New URL | Original Page |