strict-class
v1.0.2
Published
An ES5 Class, which can be extended, and supports mixins and property descriptors.
Downloads
2
Maintainers
Readme
strict-class
An ES5 Class, which can be extended, and supports mixins and property descriptors.
Usage
var Class = require('strict-class');
var Person = Class.extend({
constructor: function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
},
greet: function() {
console.log('My name is ' + this.name + '.');
},
get name() {
return this.firstName + ' ' + this.lastName;
},
set name(val) {
var parts = val.split(' ');
this.firstName = parts[0];
this.lastName = parts.slice(1).join(' ');
}
});
var john = new Person('John', 'Smith');
john.greet(); // => "My name is John Smith."
john.name = "John Brown";
john.greet(); // => "My name is John Brown."
Extending a Class
var Pirate = Person.extend({
greet: function greet() {
greet.__super.call(this);
console.log("Arrrrrrr, matey!");
}
});
var blackbeard = new Pirate('Captain', 'Blackbeard');
blackbeard.greet(); // => "My name is Captain Blackbeard. Arrrrrrr, matey!"
blackbeard.name = "Ghost of Blackbeard";
console.log(blackbeard.firstName); // => "Ghost";
Mixins
Person.addMethods({
isGhost: function() {
return /\bghost\b/i.test(this.name);
}
});
john.isGhost() // => false
blackbeard.isGhost() // => true