clazz-js
v0.5.2
Published
Portable JavaScript library for class-style OOP programming
Downloads
35
Readme
ClazzJS
ClazzJS is portable JavaScript library for class-style OOP programming. Its main goal is to provide expressive DSL to write your JavaScript programs in easy-to-understand, well-known, convenient and flexible class base manner. It's works well both on client and server sides.
Features include:
- Single inheritance
- Expressive, extensible DSL for declaring of your class
- Methods generation
- Events emitting
- Object properties changes observing
- Namespaces and more...
You'll find the example bellow to have a common idea what I'm talking about.
Documentation
This docs describe many features of the library, but not all. Use source to learn ClazzJS better. In every doc chapter there are links on respective source files.
Example
Main goal of this example is to give you a common idea about ClazzJS. It's not discover all features of the library. Online working version of this example is available on plunker: http://plnkr.co/edit/c5Xveb. Feel free to play around with it!
Person
clazz declaration:
clazz("Person", {
constants: {
SEX: ['male', 'female']
},
properties: {
name: {
type: 'string',
methods: ['get']
},
phone: {
type: ['string', {
pattern: /\d{1,2}-\d{3}-\d{5,7}/
}]
},
birthday: {
type: 'datetime',
constratins: {
inPast: function(birthday) {
return birthday.getTime() < Date.now();
}
}
},
sex: {
type: 'string',
methods: ['get', 'set', 'is'],
converters: {
toFull: function(sex) {
switch(sex.toLowerCase()) {
case 'm': sex = 'male'; break;
case 'f': sex = 'female'; break;
}
return sex;
}
},
constraints: {
existedSex: function(sex) {
return -1 !== this.const('SEX').indexOf(sex);
}
}
}
},
methods: {
getAge: function() {
return (new Date()).getFullYear() - this.getBirthday().getFullYear();
}
}
});
Teacher
clazz inherited from Person
:
clazz('Teacher', 'Person', {
constants: {
SUBJECT: ['physics', 'literature', 'mathematics']
},
properties: {
subject: {
type: 'string',
constraints: {
existedSubject: function(subject) {
return -1 !== this.const('SUBJECT').indexOf(subject);
}
}
}
}
});
Creation and manipulation of instances:
// Create just common person - John (without 'new' operator)
var john = clazz('Person').create({
name: 'John Stewart',
sex: 'M',
phone: '1-925-123567',
birthday: "1989-12-13"
});
john instanceof clazz("Person"); // true
john.getName(); // 'John Stewart'
john.getAge(); // 24
john.getSex(); // 'male'
john.getPhone(); // 1-925-123567
john.setPhone('7-925-1'); // Throw phone pattern fail error with message:
// 'Value "7-925-1" does not match
// pattern "/\d{1,2}-\d{3}-\d{5,7}/"'
john.isSex("male"); // true
john.isSex("female"); // false
john.setSex('unsupportedSex'); // Throw existedSex constraint fail error with message:
// 'Constraint "existedSex" was failed!'
john.setSex('female'); // Successfully change sex of John
john.getSex(); // 'female'
john.isSex("male"); // false
john.isSex("female"); // true
john.getBirthday() instanceof Date; // true
john.getBirthday().getMonth(); // 12
john.getBirthday().getFullYear(); // 1989
// Create math teacher - Mr. George Smith. (with 'new' operator)
var mathTeacher = new clazz('Teacher')({
name: 'George Smith',
sex: 'male',
birthday: '1973-12-34',
subject: 'mathematics'
});
mathTeacher instanceof clazz('Person'); // true
mathTeacher instanceof clazz('Teacher')); // true
mathTeacher.getName(); // John Smith
License
Copyright (c) 2013 Aleksey Podskrebyshev. Licensed under the MIT license.