Skip to content

Instantly share code, notes, and snippets.

@rektide
Last active Mar 23, 2020
Embed
What would you like to do?
Class sugar

Class sugar

This project is a reference corpus for exploring similarities and differences in conventional JavaScript inheritance ("Naive"), ES6 Classes ("Classes"), as well as an example of trying to ape classes ("Sugarfree"). It consists of a chart of JavaScript property descriptors where comparison is possible, and of reference implementations of class-like structures/systems, in:

writable enumerable configurable value diff from default if prop
Things we can do something about
Default defineProperty {value} false false false value
Naive.prototype.bing false false false [Function: bing]
Classes.prototype.bing true false true [Function: bing] {enumerable:true,value}
Classes.singleton true false true [Function: singleton] {enumerable:true,configurable:true,value}
Language behavior
Classes.prototype false false false Classes{}
Naive.prototype true false false {}
NaiveExtend.prototype true false false NaiveExtend {constructor: NaiveExtend}
Classes.protype.constructor false[1] false true [Function: Classes]
Naive.prototype.constructor true false true [Function]
NaiveExtend.prototype.constructor true true true [Function: NaiveExtend]

[1] node v5.10 does not reflect this setting


Reference

This is possible and provoked by seeing, and the desire to get a quick/fast code reference for it.

Preview of next release of “Exploring ES6” (@exploringjs): the attributes of properties created by classes. Properties chart — Axel Rauschmayer, https://twitter.com/rauschma/status/718868475100413953


Help

I am seeking ways to recreate objects with closer parity (in the language behaviors section) to Classes, without using ES6 Classes.

let Classes= require("./classes")
class ClassesExtend extends Classes{
moar(){ return "boost" }
}
module.exports= ClassesExtend
let _singleton
class Classes {
bing(){ return "bang" }
static singleton() { return _singleton || (_singleton = new Classes()) }
}
module.exports= Classes
let Classes= require("./classes"), Naive= require("./naive"), NaiveExtend= require("./NaiveExtend")
Object.getOwnPropertyDescriptor(Naive, "singleton")
Object.getOwnPropertyDescriptor(classes.prototype, "bing")
Object.getOwnPropertyDescriptor(classes, "singleton")
Object.getOwnPropertyDescriptor(Classes, "prototype")
Object.getOwnPropertyDescriptor(Naive, "prototype")
Object.getOwnPropertyDescriptor(NaiveExtend, "prototype")
Object.getOwnPropertyDescriptor(Classes.prototype, "constructor")
Object.getOwnPropertyDescriptor(Naive.prototype, "constructor")
Object.getOwnPropertyDescriptor(NaiveExtend.prototype, "constructor")
let Naive= require("./naive")
function NaiveExtend(){
Naive.call(this)
}
let moar= {value: function(){return "boost"}}
NaiveExtend.prototype= Object.create(Naive.prototype, {moar})
NaiveExtend.prorotype.constructor= NiaveExtend
module.exports= NaiveExtend
let _singleton
function Naive(){}
Object.defineProperties(Naive.prototype, {
bing: {
value: function(){ return "bang" }
}})
Object.defineProperties(Naive, {
singleton: {
value: function() { return _singleton || (_singleton = new Naive()) }
}})
module.exports= Naive
let _singleton
function Sugarfree(){}
Object.defineProperties(Sugarfree.prototype, {
bing: {
value: function(){ return "bang" },
writable: true,
enumerable: false,
configurable: true
}})
Object.defineProperties(Sugarfree, {
singleton: {
value: function() { return _singleton || (_singleton = new Sugarfree()) },
writable: true,
enumerable: false,
configurable: true
}})
module.exports= Sugarfree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment