상세 컨텐츠

본문 제목

[자바스크립트] 정적 메서드의 속성 - 좌표거리 구하기

카테고리 없음

by esoesmio 2023. 5. 7. 23:55

본문

5,5와 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

댓글 영역