plus.class
v1.0.3
Published
Class and inheritance
Downloads
6
Maintainers
Readme
Class
Should allow to create Classes
should create simple hello Class
require `src/Class.js` as `Class`
code:
"""
```
var MyClass = Class({
name: 'name1',
hello: function () {
return 'hello ' + this.name;
}
});
var instance = new MyClass();
```
"""
`instance.name` should equal 'name1'
`instance.hello()` should equal 'hello name1'
should allow to construct with methods: init, _new, _constructor, _construct
require `src/Class.js` as `Class`
code:
"""
```
var MyClass = Class({
init: function () {
this.name = 'xxx'
}
});
var instance = new MyClass();
```
"""
`instance.name` should equal 'xxx'
should allow to use factory class
require `src/Class.js` as `Class`
code:
"""
```
var object = {xx: 'an object'};
var MyClass = Class({
init: function () {
return object;
}
});
var instance = new MyClass();
```
"""
`instance` should equal `object`
should allow to extends
require `src/Class.js` as `Class`
code:
"""
```
var MyClass1 = Class({
name1: 'name1',
name2: 'name2',
hello1: function () {
return 'hello ' + this.name1;
},
hello2: function () {
return 'hello ' + this.name2;
}
});
var MyClass2 = Class(MyClass1, {
name1: 'name1-1',
hello2: function () {
return 'hello2 ' + this.name2;
}
});
```
"""
code `var instance = new MyClass1();`
`instance.hello1()` should equal 'hello name1'
`instance.hello2()` should equal 'hello name2'
`instance` should be instanceOf `MyClass1`
code `var instance2 = new MyClass2();`
`instance2.hello1()` should equal 'hello name1-1'
`instance2.hello2()` should equal 'hello2 name2'
`instance2` should be instanceOf `MyClass1`
`instance2` should be instanceOf `MyClass2`
should allow to call _super
require `src/Class.js` as `Class`
code:
"""
```
var MyClass1 = Class({
name: 'name1',
hello: function () {
return 'hello ' + this.name;
}
});
var MyClass2 = Class(MyClass1, {
hello: function () {
return this._super() + '-class2';
}
});
var MyClass3 = Class(MyClass2, {
hello: function () {
return this._super() + '-class3';
}
});
```
"""
code `var instance = new MyClass2();`
`instance.hello()` should equal 'hello name1-class2'
code `var instance = new MyClass3();`
`instance.hello()` should equal 'hello name1-class2-class3'
should allow to call _super in the constructor
require `src/Class.js` as `Class`
code:
"""
```
var MyClass1 = Class({
init: function (name3) {
this.name1 = 'name1';
this.name2 = 'name2';
this.name3 = name3;
}
});
var MyClass2 = Class(MyClass1, {
init: function () {
this._super('name3-updated');
}
});
```
"""
code `var instance = new MyClass1('name3');`
`instance.name1` should equal 'name1'
`instance.name2` should equal 'name2'
`instance.name3` should equal 'name3'
code `var instance = new MyClass2();`
`instance.name1` should equal 'name1'
`instance.name2` should equal 'name2'
`instance.name3` should equal 'name3-updated'
should copy static methods vars
require `src/Class.js` as `Class`
code:
"""
```
var MyClass1 = Class();
MyClass1.staticMethod1 = function () {
}
MyClass1.staticVar1 = 100;
var MyClass2 = Class(MyClass1);
```
"""
`MyClass2.staticMethod1 === MyClass1.staticMethod1` should be true
`MyClass2.staticVar1 === MyClass1.staticVar1` should be true
should allow to create sub classes simply
require `src/Class.js` as `Class`
code:
"""
```
var MyClass1 = Class();
var MyClass2 = Class(MyClass1);
```
"""
`new MyClass1()` should be instanceOf `MyClass1`
`new MyClass2()` should be instanceOf `MyClass1`
`new MyClass2()` should be instanceOf `MyClass2`
should construct with parent constructor
require `src/Class.js` as `Class`
code:
"""
```
var MyClass1 = Class(function () {
this.name = 'xxx';
});
var MyClass2 = Class(MyClass1);
```
"""
code `var instance = new MyClass2();`
`instance.name` should equal 'xxx'
should create class with "new"
require `src/Class.js` as `Class`
code:
"""
```
var MyClass = new Class({
init: function () {
this.hi = 'hello'
}
});
```
"""
code `var instance = new MyClass();`
`instance.hi` should equal 'hello'
10 scenarios (10 passed) 54 steps (54 passed)