JSFiddle - React, Tailwind, and code Playground

by jun68ykt

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

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

class RestrictedPoint extends Point {
    constructor(x, y, z) {
        super(x, y, z);
        this.adjust();
    }

    adjust() {
        for ( const [axis, value] of Object.entries(this)) {
            const config = this.constructor.config[axis];         
            this[axis] =  Math.min(config.max, Math.max(config.min, value));
        }
    }
}

RestrictedPoint.config = {
    x: { min: 0, max: 99 }, // X座標は0以上99以下
    y: { min: 0, max: 199 }, // Y座標は0以上199以下
    z: { min: 100, max: 399 } // Z座標は100以上399以下
};


const p1 = new Point(-1, 230, 50);
console.log(p1);

const p2 = new RestrictedPoint(-1, 230, 50);
console.log(p2);