Skip to content

Instantly share code, notes, and snippets.

@allenwb
Created June 20, 2014 20:55
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 allenwb/e4b7116aefcec840e9cf to your computer and use it in GitHub Desktop.
Save allenwb/e4b7116aefcec840e9cf to your computer and use it in GitHub Desktop.
experimenting witn alternatives new semantics for ES6
//The following is essentially the current ES6 spec. for Boolean, expressed in ES code
//$$foo calls are performing spec. level operations that don't have direct ES language equivalents
class Boolean {
static [Symbol.create](.){
//@@create allocates the object with private slots but doesn't do any slot initialization
return $$OrdinaryDateFromConstructor(this,"%BooleanPrototype%",["[[BooleanData]]"]);
// a newly created Boolean instance has undefined as the value of its [[BooleanData]] internal slot.
// the undefined value indicates that the instance has not yet been initialized by the constructor function
}
constructor (value) {
let b = !!value;
//first check if "called as functon" or "called as constructor"
if ($$HasPrivateValue(this, "[[BooleanData]]", undefined)) {
//this is the "called as a constructor" case
$$SetPrivate(obj,"[[BooleanData]]", b);
return this;
}
//this is the "called as a function" case
return b;
}
}
class Boolean {
static [Symbol.create](...args){
let obj = $$OrdinaryDateFromConstructor(this,"%BooleanPrototype%",["[[BooleanData]]"]);
let [value=undefined]=args;
$$SetPrivate(obj,"[[BooleanData]]", !!value);
return obj;
}
static [Symbol.initialize](obj,...args) {
//over-ride default call to constructor function for instance initialization
return obj;
}
constructor(value) {
return !!value;
}
}
class SubBool1 extends Boolean {
//a subclass of Boolean that inherits factory behavior but adds initialization
static [Symbol.initialize](obj,...args) {
//over-ride default call to constructor function for instance initialization
super[Symbol.initialize](obj,...args);
obj.prop1 = null;
obj.prop2 = null;
}
}
class SubBool2 extends Boolean {
//a subclass of Boolean that ireverts to normal constructor initialization
constructor(...args) {
obj.prop1 = null;
obj.prop2 = null;
}
static [Symbol.initialize](obj,...args) {
return this.apply(obj, args); //use defuault constructor initialization semantics
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment