0.6.0 • Published 10 years ago

protolib v0.6.0

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
10 years ago

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
0.6.0

10 years ago

0.5.2

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.4

11 years ago

0.4.3

11 years ago

0.4.2

11 years ago

0.4.1

11 years ago

0.4.0

11 years ago

0.3.0

11 years ago

0.2.1

11 years ago

0.2.0

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago