Skip to content

Instantly share code, notes, and snippets.

@yosiat
Last active December 20, 2015 15:08
Show Gist options
  • Save yosiat/6151579 to your computer and use it in GitHub Desktop.
Save yosiat/6151579 to your computer and use it in GitHub Desktop.
superclass
// Removed toJSON, just for beautifying :)
/* CONSTRUCTOR -> */ function expression() {
};
Object.defineProperty(expression.prototype, "_type",
{
get : function(){
return Object.getPrototypeOf(this).constructor.name;
}
});
/* CONSTRUCTOR -> */ function lambdaExpression() {
};
// Here you change the prototype of lambdaExpression, which is alright
lambdaExpression.prototype = Object.create(expression.prototype);
// But you need to set the constructor of lambdaExpression to be lambdaExpression
lambdaExpression.prototype.constructor = lambdaExpression;
/* CONSTRUCTOR -> */ function lambda3x1(body, tailCall, parameters) {
this.body = body;
this.tailCall = tailCall;
this.parameters = parameters;
};
lambda3x1.prototype = Object.create(lambdaExpression.prototype);
lambda3x1.prototype.constructor = lambda3x1;
// Create new lambda3x1, the new keyword is important
var lam = new lambda3x1("body", "tail", "params");
// get the prototype of lam, which is expression
var lamPrototype = Object.getPrototypeOf(lam);
// get the constructor - which the function decleration look at the comments ^
// which will return a Function, and get it's name
console.log(lamPrototype.constructor.name);
console.log(lam._type);
// DETAILS DOCUMENTATION - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment