createClass
Just sugar.
usage
Contrived example time! :D
var createClass = ; var Logger = ; var Animal = ; var FelineMixin = { console; }; var Cat = Animal; var cat = thinking: 'hungry' ; ;
docs
The module.exports
a function
var createClass = ;
When assigning createClass
to a function makes it the super class.
{}ConstructorcreateClass = createClass; var Child = Constructor;// behold! Child will inherit from `Constructor` now
createClass
Can take 2 arguments
arguments
Super
, typefunction
, to use as super classspec
, typeobject
, to be added to the constructor prototype. Can be either passed as 1st or 2nd argument to the function
returns a new class with the given spec
in its prototype and two static methods
NewClass.create
: creates a new instance (up to 3 arguments)NewClass.createClass
: same ascreateClass
but usingNewClass
as super
example:
var Animal = ; var Cat = ; var cat = Cat; cat;// => meoww
spec
has some special properties
spec.create
Should be a Function
.
If given, it will be used the constructor function for the returned class.
The module is using inherits
to do inheritance which means: super_
is set as a static property.
var Animal = ;
and since Super
can be passed as first argument
var Cat = ; var cat = Cat; console;// => { thinking: 'hungry' }
spec.mixins
Same as merge
/extend
/Object.assign
with the resulting class prototype. The element at the tail of the array is the first to be used and from there back to the beginning of the array.
Each element of the Array
can be object
or function
.
When an element is ...
object
, its properties are directly assignedfunction
, its prototype (enumerable) properties are assigned
In both cases there is no method override. If was defined on already it will not be overridden.
{}Animalprototype { console;}; {}Mammalprototype { return true;}; var AnimalMixin = { console; ; }; var Bear = ; var Cat = ;
Mixins are good looking because you can put them together with just objects, and one may think there is less of the "you wanted a bannana but you got a gorilla holding a banana" problem, but there are still issues with them see: mixins are considered harmful.
So we should be careful grasshoppers.
spec.statics
Object of static properties to add to the new class.
var Animal = ;
install
With npm
npm install --save-dev createClass
license
The MIT License (MIT)
Copyright (c) 2016-present Javier Carrillo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.