Skip to content

Instantly share code, notes, and snippets.

@pimoGit
Created December 4, 2019 14:15
Show Gist options
  • Save pimoGit/baf3f59455567e39c8ce90afca7e3ec8 to your computer and use it in GitHub Desktop.
Save pimoGit/baf3f59455567e39c8ce90afca7e3ec8 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/tesedov
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// static methods on Classes
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.hypot(dx, dy);
}
//could I do like this also (so with a NONstatic method)? [YEP]
distanceint(b) {
const dx = this.x - b.x;
const dy = this.y - b.y;
return Math.hypot(dx, dy);
}
}
const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
console.log(p1.distance); //undefined
console.log(p2.distance); //undefined
console.log(p1.distanceint); //function
console.log(p2.distanceint); //function
console.log("static method return: ",Point.distance(p1, p2)); // 7.0710678118654755
console.log("NONstatic method return: ",p1.distanceint(p2)); // 7.0710678118654755
// static methods are accessible even in the subclasses? [YEP]
class subPoint extends Point {
}
console.log("static method return in subclass: ",subPoint.distance(p1, p2)); // 7.0710678118654755
</script>
<script id="jsbin-source-javascript" type="text/javascript">// static methods on Classes
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.hypot(dx, dy);
}
//could I do like this also (so with a NONstatic method)? [YEP]
distanceint(b) {
const dx = this.x - b.x;
const dy = this.y - b.y;
return Math.hypot(dx, dy);
}
}
const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
console.log(p1.distance); //undefined
console.log(p2.distance); //undefined
console.log(p1.distanceint); //function
console.log(p2.distanceint); //function
console.log("static method return: ",Point.distance(p1, p2)); // 7.0710678118654755
console.log("NONstatic method return: ",p1.distanceint(p2)); // 7.0710678118654755
// static methods are accessible even in the subclasses? [YEP]
class subPoint extends Point {
}
console.log("static method return in subclass: ",subPoint.distance(p1, p2)); // 7.0710678118654755
</script></body>
</html>
// static methods on Classes
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.hypot(dx, dy);
}
//could I do like this also (so with a NONstatic method)? [YEP]
distanceint(b) {
const dx = this.x - b.x;
const dy = this.y - b.y;
return Math.hypot(dx, dy);
}
}
const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
console.log(p1.distance); //undefined
console.log(p2.distance); //undefined
console.log(p1.distanceint); //function
console.log(p2.distanceint); //function
console.log("static method return: ",Point.distance(p1, p2)); // 7.0710678118654755
console.log("NONstatic method return: ",p1.distanceint(p2)); // 7.0710678118654755
// static methods are accessible even in the subclasses? [YEP]
class subPoint extends Point {
}
console.log("static method return in subclass: ",subPoint.distance(p1, p2)); // 7.0710678118654755
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment