Skip to content

Instantly share code, notes, and snippets.

@friedbrice
Last active December 14, 2016 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friedbrice/9b76ab6e3186a9f1d08fba09a4b5ff9d to your computer and use it in GitHub Desktop.
Save friedbrice/9b76ab6e3186a9f1d08fba09a4b5ff9d to your computer and use it in GitHub Desktop.
ES5 Classes vs. ES6 Classes (vs. Java Classes vs. Scala Classes)
/* ES5-style class declaration */
function ParsedTime(hour, minute, second) {
this.hour = hour
this.minute = minute
this.second = second
this.toString = function () {
return JSON.stringify(this)
}
}
ParsedTime.fromDate = function (date) {
return new ParsedTime(date.getHours(), date.getMinutes(), date.getSeconds())
}
/* ES6-style class declaration */
class ParsedTime {
constructor(hour, minute, second) {
this.hour = hour
this.minute = minute
this.second = second
}
toString() {
return JSON.stringify(this)
}
static fromDate(date) {
return new ParsedTime(date.getHours(), date.getMinutes(), date.getSeconds())
}
}
/* Java class declaration */
class ParsedTime {
int hour;
int minute;
int second;
public ParsedTime(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public String toString() {
return JSON.stringify(this);
}
public static ParsedTime fromDate(Date date) {
return new ParsedTime(date.getHours(), date.getMinutes(), date.getSeconds());
}
}
/* Scala class declaration */
class ParsedTime(hour: Int, minute: Int, second: Int) {
def toString() = JSON.stringify(this)
}
object ParsedTime {
def fromDate(date: Date) = new ParsedTime(date.getHours(), date.getMinutes(), date.getSeconds())
}
/* Haskell something-or-other */
data ParsedTime = ParsedTime { hour :: Int, minute :: Int, second :: Int } deriving Show
parsedTimeFromDate :: Date -> ParsedTime
parsedTimeFromDate date = ParsedTime (getHours date) (getMinutes date) (getSeconds date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment