prototype-less

0.1.2 • Public • Published

prototype-less

NPM

NPM version David David NPM license Omniref flattr

The whole package was inspired by a post by Reginald “Raganwald” Braithwaite: Mixins, Forwarding, and Delegation in JavaScript

Provides helper functions to structure software using prototype-less techniques: mixin, forwarding and delegation.

The general idea is to separate the model domain from the implementation domain: separate properties (data) from behaviours (methods).

Installation

Node Dependency

Execute following line

npm install prototype-less@0.0.x --save

Require module

var pless = require('prototype-less');

Methods

Method Matrix (used context to resolving time)

Used Context Early Bound Late Bound Later Bound (State Machines)
BaseObject mixin delegate dynamicDelegate
MetaObject privateMixin forward dynamicForward

mixin

Parameters:

  • baseObject: some Object to be extended.
  • metaObject: the template to be applied to the baseObject.

Our person object is a template (metaobject), it provides some functionality to be mixed into an object.

// extends a domain with behaviours,
// but behaviours access and modifies baseobject's properties
pless.mixin(raganwald, person);

privateMixin

Parameters:

  • baseObject: some Object to be extended.
  • metaObject: the template to be applied to the baseObject.

Same as mixin but the properties related to the injected behavior are kept private.

var hasCareer = {
  career: function () {
    return this.chosenCareer;
  },
  setCareer: function (career) {
    this.chosenCareer = career;
    return this;
  },
  chosenCareer: 'unemployed' // initial value
};

// extends a domain with behaviours,
// but keeps behaviour specific properties private
// example here: this.chosenCareer
// the property won't be attached to Object "raganwald"
pless.privateMixin(raganwald, hasCareer);

forward

Parameters:

  • baseObject: object issuing the call.
  • metaObject: object providing behaviour and context.

Creates a relationship between Objects. Here the function is provided by the metaobject and it is executed using the metaobject's context.

delegate

Parameters:

  • baseObject: object issuing the call and providing context.
  • metaObject: object providing behaviour.

Creates a relationship between Objects. Here the function is provided by the metaobject and it is executed using the baseobject's context.

dynamicForward

Parameters:

  • baseObject: object issuing the call.
  • metaObject: object providing behaviour and context.
  • propertyName: baseObject property to which the metaobject will be bound.

Same as forward but the target of the forwarding (metaobject) is late bound: it is solved at run-time and opens applications for modeling classes of behaviour that change dynamically (as state machines).

dynamicDelegate

Parameters:

  • baseObject: object issuing the call and providing context.
  • metaObject: object providing behaviour.
  • propertyName: baseObject property to which the metaobject will be bound.

Same as delegate but the target of the delegation (metaobject) is late bound: it is solved at run-time and opens applications for modeling classes of behaviour that change dynamically (as state machines).


Copyright (c) 2015 Luscus (luscus.redbeard@gmail.com)

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.

Package Sidebar

Install

npm i prototype-less

Weekly Downloads

1

Version

0.1.2

License

MIT

Last publish

Collaborators

  • luscus