-
-
Save Raynos/2151045 to your computer and use it in GitHub Desktop.
A minimal class proposal for ECMAScript.next
This file contains hidden or 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
// Roughly: a combination of Jeremy’s and Allen’s ideas, updated with the results of recent discussions | |
// Guidelines: | |
// - Don’t use the same syntax as for object literals, but stay close. | |
// - Rationale: Some object literal features are forbidden in class declarations => don’t confuse people | |
// - Rationale: Comma separation is a bit tricky. | |
// - Keep new features at a minimum | |
// - 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. | |
// - In order to minimize confusion, keep module syntax and class declaration syntax similar. | |
// Rules: | |
// - Use @ to refer to name objects | |
// - there is no syntactic sugar for `this.` | |
// Name objects: | |
// - Rough rule 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 Monster extends Being { | |
// Only methods are allowed here (because these are the prototype’s properties) | |
// Create name objects that only exist within the declaration’s scope | |
private age, health, incAge; | |
// Constructor | |
// Alternative: use the name "new". Caveat: super.new(name) looks a bit weird. | |
constructor(name, this.weight, this.@age, this.@health) { | |
super.constructor(name); | |
} | |
/* Desugared: | |
constructor(name, weight, age, health) { | |
super.constructor(name); | |
this.weight = weight; | |
this.@age = age; | |
this.@health = health; | |
} | |
*/ | |
getNameObjectForAge() { | |
return age; | |
} | |
// private method | |
@incAge() { | |
this.@age++; | |
} | |
// getter | |
get strength() { | |
// stronger with increasing age... | |
return super.strength * this.@age; | |
} | |
} |
This file contains hidden or 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
let Monster = (function () { | |
module name from "@name" | |
let age = name.create(), | |
health = name.create(), | |
incAge = name.create() | |
function Monster(name, weight, age, health) { | |
super.constructor(name) | |
this.weight = weight | |
this[age] = age | |
this[health] = health | |
} | |
Monster.prototype = Object.create(Being.prototype, { | |
constructor: { | |
value: Monster, | |
configurable: true, | |
writable: true | |
} | |
}) | |
Monster.prototype.getNameObjectForAge = function () { | |
return age | |
} | |
Monster.prototype[incAge] = function () { | |
this[age]++ | |
} | |
Object.defineProperty(Monster.prototype, "strength", { | |
get: function () { | |
return super.strength + this[age] | |
}, | |
enumerable: true, | |
configurable: true | |
}) | |
return Monster | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment