Socket
Socket
Sign inDemoInstall

class-wrapper

Package Overview
Dependencies
0
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

class-wrapper


Version published
Weekly downloads
59
increased by37.21%
Maintainers
1
Install size
67.1 kB
Created
Weekly downloads
 

Readme

Source

{class}

Set of wrappers for easier definition of different kind of classes. Currently there are three kinds of wrappers:

  • ClassBuilder - the main wrapper that realize the inheritance and data encapsulation. It requires a function that defines how the instances will be created. It returns the clone of defined instance builder function with special features and properties.
  • Class - it has predefined instance builder function for ClassBuilder, that calls all parent's constructors before calling the constructor of a new class
  • SingletonClass - it has predefined instance builder function for ClassBuilder, that by any new always returns the same instance. The first instance will be created in the same way as Class

Each wrapper accepts:

  • the parent class from which a new class is going to be derived
  • constructor function for a new class
  • object of properties and methods for a new class

Defined class properties are treated as default values for a new instance and they are isolated between instances. For example if some class has an object in properties then each instance will have its own copy of that default object. Only methods are shared.

Requirements

  • Object.create
  • Function.prototype.bind
  • Array.prototype.forEach

If this library is planned to be used in some legacy environment then please add required plyfills. Here is a good resource: https://github.com/es-shims/es5-shim

Installation

Via NPM (Node Package Manager)

$ npm install class-wrapper --save

Available library files:

  1. dest/class-wrapper.js - not minified library package
  2. dest/class-wrapper.min.js - minified library package

The destination library files are surrounded with the universal module definition. So it can be loaded

  • as a module for NodeJS
  • as an AMD module
  • or will be stored into the global variable under the name "class-wapper"

Simple usage examples

// Define a Figure class:
var Figure = Class(function() {
	console.log('Figure::initialize()');
}, {
	// abstract function for a calculating a suqare of a figure
	calcArea: function() {}
});

// Define Rectangle class derived from a Figure class:
var Rectangle = Class(Figure, function(width, height) {
	console.log('Rectangle::initialize()');

	this._width = width;
	this._height = height;
}, {
	_width: 0,
	_height: 0,

	calcArea: function() {
		return this._width * this._height;
	}
});

// Define Square class as a special case of Rectangle:
var Square = Class(Rectangle, function(length) {
	console.log('Square::initialize()');

	this._height = length;
});


// Create a rectangle with width 2 and height 4
var someRect = new Rectangle(2, 4);
// the following lines will be logged in console:
//	Figure::initialize()
//	Rectangle::initialize()

console.log(someRect.calcArea());		// the area is: 8


// Create a square with an edge length 5
var someSqrt = new Square(5);
// the following lines will be logged in console:
//	Figure::initialize()
//	Rectangle::initialize()
//	Square::initialize()

console.log(someSqrt.calcArea());		// the area is: 25

Keywords

FAQs

Last updated on 14 Apr 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc