Last active
May 17, 2024 03:37
-
-
Save PatrickJS/bec4889e0a5785cae2a9 to your computer and use it in GitHub Desktop.
Different examples of OOP "class" with "inheritance" done using JavaScript including languages that transpile into js. Take notice to the amount of boilerplate that's needed in ES5 compared to ES6. These examples all have the same interface with pros/cons for each pattern. If they seem similar that's whole point especially the difference between…
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
var EventEmitter = require('events').EventEmitter; | |
// Prototypal | |
function Person() { | |
var person = Object.create(Person.prototype); | |
EventEmitter.call(person); | |
person.wallet = 0; | |
return person; | |
} | |
Person.prototype = Object.create(EventEmitter.prototype); | |
Person.prototype.talk = function() { | |
}; | |
var person = Person(); | |
person.talk(); |
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
import { EventEmitter } from 'events'; | |
// AtScript pseudo-classical | |
class Person extends EventEmitter { | |
wallet:int; | |
constructor() { | |
super(); | |
this.wallet = 0; | |
} | |
talk() { | |
} | |
} | |
var person = new Person(); | |
person.talk(); |
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
{EventEmitter} = require 'events' | |
# Coffeescript pseudo-classical | |
class Person extends EventEmitter | |
constructor: -> | |
super() | |
@wallet = 0 | |
talk: -> | |
person = new Person() | |
person.talk() |
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
import 'package:event_emitter/event_emitter.dart'; | |
/// Dart pseudo-classical | |
class Person extends EventEmitter { | |
num wallet; | |
Person() { | |
this.wallet = 0; | |
} | |
void talk() { | |
} | |
} | |
var person = new Person(); | |
person.talk(); |
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
var EventEmitter = require('events').EventEmitter; | |
// ES5 Pseudo-classical | |
function Person() { | |
EventEmitter.call(this); | |
this.wallet = 0; | |
} | |
Person.prototype = Object.create(EventEmitter.prototype); | |
Person.constructor = Person; | |
Person.prototype.talk = function() { | |
}; | |
var person = new Person(); | |
person.talk(); |
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
import { EventEmitter } from 'events'; | |
// ES6 pseudo-classical | |
class Person extends EventEmitter { | |
constructor() { | |
super(); | |
this.wallet = 0; | |
} | |
talk() { | |
} | |
} | |
let person = new Person(); | |
person.talk(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there typo?
);
https://gist.github.com/gdi2290/bec4889e0a5785cae2a9#file-factory-shared-es6-js-L1