Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from rauschma/class.js
Created November 3, 2011 18:14
Show Gist options
  • Save rwaldron/1337250 to your computer and use it in GitHub Desktop.
Save rwaldron/1337250 to your computer and use it in GitHub Desktop.
Yet another simple class proposal
// Guidelines:
// - Class declarations and object literals should have the same features
// - Keep new features at a minimum (apart from what’s already in the object literal proposal)
// - Don’t obscure the fact that private names are actually name objects.
// => They can also be imported from somewhere else – a use case that needs to be supported.
// Rules:
// - Use # to denote const-ness
// Alternative: keyword `const`
// - Use @ to refer to name objects
// - there is no syntactic sugar for `this.`
// Name objects:
// - Rough image for what @foo means: “insert arbitrary identifier here”.
// - Used for property access and to name methods.
// - foo.@bar is syntactic sugar for foo[bar]
// - Rationale: immediately obvious that it’s property access
class Being <| Monster {
// non-function prototype properties are forbidden
// Generate name objects
private {
age, health, incAge
}
constructor(name, weight, age, health) {
super.constructor(name);
this.weight = weight;
this.@age = age;
this.@health = health;
}
/* Sugared:
constructor(name, this.weight, this.@age, this.@health) {
super.constructor(name);
}
*/
getNameObjectForAge() {
return age;
}
// private method
@incAge() {
this.@age++;
}
// getter
get strength() {
// stronger with increasing age...
return super.strength * this.@age;
}
}.{
// class properties
// (possibility: section static { ... })
#MAX_STRENGTH: 400 // constant
}
//////// Options for the subtyping and expression syntax
//// Subtyping syntax: <|
// declarations
class Being <| Monster { ... }
class Monster { ... }
// expressions
let Monster = Being <| class { ... }
let Monster = class { ... }
//// Subtyping syntax: proto
// declarations
class Being proto Monster { ... }
class Monster { ... }
// expressions
let Monster = Being proto class { ... }
let Monster = class { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment