
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Lightweight utility module for creating javascript classes.
copyright xkcd
Ok, Javascript already uses OOP, but based in prototypes. It is not bad, but occasionally the class system is the best approach. This library help you to create "raw vanilla" Javascript classes, for the best integrating with another codes and libraries with the best performance.
#Features
#Quick Example
var def = require('def')
var Point = def({
new: function (x, y) {
this.x = x;
this.y = y;
},
distance: function (p) {
return Math.sqrt(
Math.pow(p.x - this.x, 2) +
Math.pow(p.y - this.y, 2)
)
}
})
var a = new Point(2, 4)
var b = new Point(5, -6)
a.distance(b);
// -> 10.44030650891055
#Installation npm:
$ npm install lang-def
$ bower install lang-def
Or simply download.
##Import
###In CommonJS (like node.js)
var def = require('lang-def')
###In AMD (like requireJS)
require.config({
paths: {
def: 'libs/lang-def/def'
}
});
Then simply use
require(['def'], function (def) {
// use def
});
###In vanilla (like simple web page)
<script src="my_path/lang-def/def.js"></script>
<script>
// use window.def or simply def
</script>
#API ##def Creates a constructor function (Class).
Signature:
def([ string name ], [ function BaseClass ], [ array mixins ], [ object props ])
-> Function
undefined
, but resolved to 'AnonymousConstructor'
.'Person'
, 'SuperPerson'
. Not 'persons.Person'
or 2Person
.new Function(...)
to generate the name
in the runtime.undefined
, by definition inherits from Object
.def
or by vanilla javascript.[ ]
{ }
constructor
.
function () { def.mixin(this, arguments); }
.methodName
of Class
in the this
clausure.#Examples ##A little Game Egine
See in jsfiddle.net/mnrtag0v/8
.
##Classic examples ###Inheritance
var Animal = def({
name: null,
speak: function () { return 'I am ' + this.name; }
});
var Cat = def(Animal, {
speak: function() {
return this.fn(Animal, 'speak') + ', the Cat';
}
});
var Dog = def(Animal, {
speak: function() {
return this.fn(Animall, 'speak') + ', the Dog';
}
});
var cat = new Cat({ name: 'Canela' });
var dog = new Dog({ name: 'Doky' });
cat instanceof Cat;
// -> true
cat instanceof Animal;
// -> true
dog instanceof Dog;
// -> true
dog instanceof Animal;
// -> true
cat.speak();
// -> 'I am Canela, the Cat'
###Mixins Example inspired from dgrid
var Widget = def({ ... });
var Grid = def(Widget, { ... });
var Sortable = def({ ... });
var Selectable = def({ ... });
var Resizable = def({ ... });
// Then we need a sortable grid
var MySortableGrid = def(Grid, [Sortable]);
// Then we need a sortable and selectable grid
var MySortableAndResizable = def(Grid, [Sortable, Selectable]);
// one grid
var grid = new MySortableAndResizable({ ... });
grid instanceof Grid; // -> true
grid instanceof Widget; // -> true
grid instanceof Sortable; // -> false
grid instanceof Selectable; // -> false
def.instanceOf(grid, Grid); // -> true
def.instanceOf(grid, Widget); // -> true
def.instanceOf(grid, Sortable); // -> true, because is Mixin
def.instanceOf(grid, Selectable); // -> true, because is Mixin
def.mixinOf(grid, Grid); // -> false
def.mixinOf(grid, Widget); // -> false
def.mixinOf(grid, Sortable); // -> true
def.mixinOf(grid, Selectable); // -> true
##def.mixin Mixin arg0 with arg1, then with arg2 (if exits), etc.
Signature:
def.mixin(object arg0 [, object arg1 [, ... ]]) -> object
The arg0 object is mutated and returned.
##def.mixinOf Check if instance is mixin of Mixin.
Signature:
def.mixinOf(object instance, function Mixin) -> bool
##def.instanceOf Check if instance is instance or mixin of MixinOrClass.
Signature:
def.instanceOf(object instance, function MixinOrClass) -> bool
#Best Practies & Tips
def
with vanilla constructor functions and in library proyects. def
just construct a vanilla constructor, so is not intrusive._
for private members. Example _lastPosition: 5
.
We can over engineering this lib for have a real private concept, but
then we like some way for made reflection... so, KISS is the better.XHR = def(...); XHR.TYPE_OK = 1; XHR.TYPE_ERR = 0
.#TODO
#LICENSE MIT LICENSE
FAQs
Lightweight utility module for creating javascript classes.
We found that lang-def demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.