protolib
v0.6.0
Published
Javascript library for doing Object-Oriented programming using the prototype system
Downloads
22
Maintainers
Readme
protolib
protolib is a Javascript library for doing Object Oriented programming using the prototype system. It is based on the article Javascript OO Without Constructors by Toby Ho.
Install
NPM
To install protolib into a Node.js project, input the following command in the root directory of your project.
npm install protolib --save
Bower
To install protolib into a client-side project using Bower, input the following command in the root directory of your project.
bower install protolib --save
Use
protolib provides several functions to help with Object Orientation.
clone(object)
This method returns an object with all the properties of the parameter object
.
NOTE: This method will create copies of not only the input object but also every object and array in its prototype chain. It can even handle circular dependencies.
Example
var protolib = require('protolib');
var object = {
name: 'Philip'
, hello: function() {
console.log('Hello, my name is ' + this.name);
}
, foo: {
bar: {
baz: [ 'test' ]
}
}
};
var object_clone = protolib.clone(object);
object_clone.foo.bar.baz[1] = object;
object_clone.foo.bar.baz[1].hello(); // outputs 'Hello, my name is Philip'
inherit(proto)
This method returns an object that inherits from (i.e its prototype is set to) proto
Example
var protolib = require('protolib');
var proto = {foo: 'bar'};
var object = protolib.inherit(proto);
console.log(object.foo); // prints 'bar'
mixin(object, prototype)
This method copies all the properties of proto
to object
.
NOTE: This method will overwrite any properties of object
that bear the same name as a property of proto
Example
var protolib = require('protolib');
var proto = {type: 'list', values: [1, 2, 3]};
var object = {readonly: true};
protolib.mixin(object, proto);
console.log(JSON.stringify(object, null, 2));
Output:
{
readonly: true,
type: "list",
values: [1, 2, 3]
}
Test
To run the unit tests, input the following command in the project's root directory.
npm test