Last active
April 13, 2020 17:14
-
-
Save jfet97/28712319a79e041cabbfda02c797edd1 to your computer and use it in GitHub Desktop.
ES6, niente this, niente weakmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Point = (x = 0, y = 0) => ({ | |
setCartesianCoordinates(_x, _y) { | |
x = _x; y = _y; | |
}, | |
setPolarCoordinates(radius, angle) { | |
x = radius * Math.cos(angle); | |
y = radius * Math.sin(angle); | |
}, | |
getCartesianCoordinates() { | |
return { x, y } | |
}, | |
getPolarCoordinates() { | |
const radius = Math.sqrt(x**2 + y**2) | |
const angle = | |
x === 0 ? y > 0 ? Math.PI/2 : -Math.PI/2 : Math.atan(y/x) + ((y < 0) ? Math.PI : 0) | |
return { radius, angle } | |
}, | |
}); | |
const point = Point(10, 10); | |
console.log("a point: ", point) | |
console.log(point.getCartesianCoordinates()) | |
console.log(point.getPolarCoordinates()) | |
point.setCartesianCoordinates(-10, 7) | |
console.log(point.getPolarCoordinates()) | |
point.setPolarCoordinates(10, Math.PI/7) | |
console.log(point.getCartesianCoordinates()) | |
console.log("----------------------------------------------------------") | |
console.log("----------------------------------------------------------") | |
console.log("----------------------------------------------------------") | |
const ImmutablePoint = (x = 0, y = 0) => ({ | |
setCartesianCoordinates(_x, _y) { | |
return ImmutablePoint(_x, _y) | |
}, | |
setPolarCoordinates(radius, angle) { | |
const _x = radius * Math.cos(angle); | |
const _y = radius * Math.sin(angle); | |
return ImmutablePoint(_x, _y) | |
}, | |
getCartesianCoordinates() { | |
return { x, y } | |
}, | |
getPolarCoordinates() { | |
const radius = Math.sqrt(x**2 + y**2) | |
const angle = | |
x === 0 ? y > 0 ? Math.PI/2 : -Math.PI/2 : Math.atan(y/x) + ((y < 0) ? Math.PI : 0) | |
return { radius, angle } | |
}, | |
}); | |
const immutablePoint = ImmutablePoint(10, 10); | |
console.log(immutablePoint) | |
console.log(immutablePoint.getCartesianCoordinates()) | |
console.log(immutablePoint.getPolarCoordinates()) | |
console.log("it creates a new point: ", immutablePoint.setCartesianCoordinates(-10, 7).getPolarCoordinates()) | |
console.log("the previous was not affected: ", immutablePoint.getPolarCoordinates()) | |
console.log("----------------------------------------------------------") | |
console.log("----------------------------------------------------------") | |
console.log("----------------------------------------------------------") | |
const COORDINATES = Object.freeze({ | |
CARTESIAN: "CARTESIAN", | |
POLAR: "POLAR" | |
}) | |
const ImmutableSuperPoint = (xr = 0, ya = 0, mode = COORDINATES.CARTESIAN) => { | |
let x, y | |
if(mode === COORDINATES.POLAR) { | |
x = xr * Math.cos(ya); | |
y = xr * Math.sin(ya); | |
} else if(mode === COORDINATES.CARTESIAN) { | |
x = xr; | |
y = ya; | |
} else { | |
throw new Error(`${mode} is not a valid coordinate system`) | |
} | |
return { | |
setCartesianCoordinates(_x, _y) { | |
return ImmutableSuperPoint(_x, _y) | |
}, | |
setPolarCoordinates(radius, angle) { | |
const _x = radius * Math.cos(angle); | |
const _y = radius * Math.sin(angle); | |
return ImmutableSuperPoint(_x, _y) | |
}, | |
getCartesianCoordinates() { | |
return { x, y } | |
}, | |
getPolarCoordinates() { | |
const radius = Math.sqrt(x**2 + y**2) | |
const angle = | |
x === 0 ? y > 0 ? Math.PI/2 : -Math.PI/2 : Math.atan(y/x) + ((y < 0) ? Math.PI : 0) | |
return { radius, angle } | |
}, | |
}}; | |
const isp1 = ImmutableSuperPoint(50, 50, COORDINATES.CARTESIAN) | |
const isp2 = ImmutableSuperPoint(20, Math.PI/4, COORDINATES.POLAR) | |
console.log(isp1.getPolarCoordinates()) | |
console.log(isp2.getCartesianCoordinates()) | |
// WARNING | |
// Ogni entità creata possederà, per forza di cose, una copia dei 4 metodi | |
// Se creo un miliardo di punti, ciaone memoria! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment