Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created May 13, 2012 04:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rauschma/2680473 to your computer and use it in GitHub Desktop.
Save rauschma/2680473 to your computer and use it in GitHub Desktop.
Class declarations with object exemplar semantics
//----- Example code:
// Class declarations:
// - No data properties allowed
// - Future: support mixins
// - Optional: call the initialization method `new` instead of `constructor`
// Superclass
class Person {
constructor(name) {
this.name = name;
}
describe() {
return "Person called "+this.name;
}
}
// Subclass
class Employee extends Person {
constructor(name, title) {
super.constructor(name);
this.title = title;
}
describe() {
return super.describe()+" ("+this.title+")";
}
});
var jane = new Employee("Jane", "CTO");
console.log(jane instanceof Person); // true
//----- Desugars to:
// Advantages:
// - You can always write Person.* instead of Person.prototype.*
// - Subtyping becomes very easy
// More information: http://www.2ality.com/2011/06/prototypes-as-classes.html
var Person = {
constructor: function (name) {
this.name = name;
},
describe: function() {
return "Person called "+this.name;
}
};
var Employee = Object.create(Person);
Employee.constructor = function (name, title) {
Employee.super.constructor.call(this, name);
this.title = title;
};
Employee.describe = function () {
return Employee.super.describe.call(this)+" ("+this.title+")";
};
var jane = Object.create(Employee);
jane.constructor("Jane", "CTO");
console.log(Person.isPrototypeOf(jane));
//----- Same functionality, via the library Proto.js, https://github.com/rauschma/proto-js
var Person = Proto.extend({
constructor: function (name) {
this.name = name;
},
describe: function() {
return "Person called "+this.name;
}
});
var Employee = Person.extend({
constructor: function (name, title) {
Employee.super.constructor.call(this, name);
this.title = title;
},
describe: function () {
return Employee.super.describe.call(this)+" ("+this.title+")";
}
});
var jane = Employee.new("Jane", "CTO");
console.log(Person.isPrototypeOf(jane));
//----- Compatibility with (legacy) constructor functions
// - To extend a constructor function Foo, you write: `extends Foo.prototype`
// Could be handled automatically, by checking whether the operand of `extends` is a function.
// Proto.js comes with support for extending constructor functions.
@k33g
Copy link

k33g commented May 13, 2012

:) thank you for your answer(s).

"their names are only visible to themselves, not anywhere else."
But i can test like this :

jane.constructor.name
//or :
jane.__proto__
// and (i know, not very nice)
Object.prototype.getTypeName = function () { return this.constructor.name; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment