# igneousjs

> A fully featured ES5 implementation for OOP in javascript for the browser or NodeJS!

Latest version **0.2.6** (published 2015-02-27) · 0 weekly downloads

## Install

```sh
npm install igneousjs
pnpm add igneousjs
yarn add igneousjs
bun add igneousjs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.6 |
| Published | 2015-02-27 |
| First published | 2015-02-17 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jim Buck |
| Maintainers | jimmyboh |
| Keywords | enum, class, oop, browser, node, object oriented programming |

## Links

- npm: https://www.npmjs.com/package/igneousjs
- Repository: https://github.com/JimmyBoh/IgneousJS
- Issues: https://github.com/JimmyBoh/IgneousJS/issues
- npm.io page: https://npm.io/package/igneousjs

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.2.6 (latest) — 2015-02-27
- 0.2.5 — 2015-02-27
- 0.2.4 — 2015-02-26
- 0.1.2 — 2015-02-19
- 0.1.0 — 2015-02-17

## README

# IgneousJS

A fully featured ES5 implementation for OOP in javascript for the browser or NodeJS!

[![NPM info](https://nodei.co/npm/igneousjs.png?downloads=true)](https://www.npmjs.com/package/igneousjs)

[![Build Status](https://travis-ci.org/JimmyBoh/IgneousJS.svg?branch=master)](https://travis-ci.org/JimmyBoh/IgneousJS)

The main goal of this library is to provide a small but powerful implementation of Object Oriented constructs in JavaScript (ES5). With ES6 on the way, this library is meant to bridge the gap by using as close to the correct syntax as possible.

## Installation:

### Node.js:

```
npm install --save igneousjs
```

You can then access it like so:

```js

var ig = require('igneousjs'),
  Enum = ig.Enum,
  Class = ig.Class;

// OR...

var Enum = require('igneousjs/enum');
var Class = require('igneousjs/class');

```

### Browser:

#### Using Bower:

```
bower install igneousjs --save
```

#### Traditional:

The distributable files are located [here](https://github.com/JimmyBoh/igneous/tree/master/dist).

Once it is loaded into the webpage, `Class` and `Enum` are available in the global scope!

Using AMD or CommonJS? No problem, it supports all types by default! (thanks to [UMD](https://github.com/umdjs/umd)) 

## Usage:

### Enums:

```

var LightSwitch = Enum.extend('Off', 'On');

var kitchenLight = LightSwitch.On;

console.log(kitchenLight === LightSwitch.On);    // => true
console.log(kitchenLight);                       // => 1
console.log(LightSwitch[kitchenLight]);          // => 'On';
console.log(LightSwitch.toString(kitchenLight)); // => 'On';

for(var state in LightSwitch){
  if(kitchenLight == state) {
    console.log('The light is ' + LightSwitch[state]);
  }
}

var Direction = Enum.extend({
  'North': 1,
  'South': 2,
  'East':  4,
  'West':  8
});

var myHeading = Direction.North | Direction.East;

console.log(myHeading);            // => 5
console.log(Direction[myHeading]); // => undefined

console.log(Enum.hasFlag(myHeading, Direction.North)); // => true
console.log(Enum.hasFlag(myHeading, Direction.West));  // => false

```

### Classes:

```

var Polygon = Class.extend({

  // Class constructor, ran during instantiation.
  constructor: function (height, width) { 
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  },

  // Class method, `this` being the class instance.
  sayName: function () {
    console.log('Hi, I am a', this.name + '.');
  }
});

var Rectangle = Polygon.extend({
  constructor: function (height, width) {
    this.super(height, width); // Call the parent method with `this.super`.
    this.name = 'Rectangle';
  },

  area: function () {
    return this.height * this.width;
  }
});

var Square = Rectangle.extend({
  constructor: function (length) {
    this.super(length, length);
    this.name = 'Square';
  },

  area: function () {
    return this.super();
  },

  sayName: function () {
    this.super(); // This super call overrides Polygon's.
    console.log('And I\'m way better than a Rectangle!');
  }
});

var p = new Polygon(4, 3);
var r = new Rectangle(2, 5);
var s = new Square(5);

p.sayName(); // => Hi, I am a Polygon.
r.sayName(); // => Hi, I am a Rectangle.
s.sayName(); // => Hi, I am a Square.
             //    And I'm way better than a Rectangle!

console.log(r.area())  // => 10			 
console.log(s.area()); // => 25

console.log(s instanceof Rectangle); // => true
console.log(r instanceof Square); // => false

```


## API

### Enum

#### `Enum.extend(obj)`
 - Returns: `Enum`,
 - Accepts: 
  - `obj`: `Object`
 
Creates a new enum from an object hash.

#### `Enum.extend(arr)`
 - Returns: `Enum`,
 - Accepts: 
  - `arr`: `Array`
 
Creates a new enum from an array of strings.

#### `Enum.extend(str1, str2, ...)`
 - Returns: `Enum`,
 - Accepts: 
  - `str`: `String` (one or many)
  
Creates a new enum from a set of strings.

#### `<enum>.toString(val)`
 - Returns: `String`,
 - Accepts: 
  - `val`: `Integer` (Enum value)
  
Returns the string value (key) based on the enum value (integer).

#### `Enum.hasFlag(enumValue, flagValue)`
 - Returns: `Boolean`,
 - Accepts: 
  - `enumValue`: `Integer` (Enum Value, your variable)
  - `flagValue`: `Integer` (Enum Value, the flag to check for)
  
Runs a bitwise comparison to see if the enum has a specified flag.

### Class

#### `Class.extend(obj)`
 - Returns: `Class`,
 - Accepts: 
  - `obj`: `Object`

Creates a new Class type. `obj` accepts a `constructor` function to act as the constructor. 
Each method will be run in the context of the class instance, exposing a `super` method that calls the parent instance of the current function.
 
See [John Resig's original blog post][2] for more information. 

#### `<class>.extend(obj)`
 - Returns: `Class`,
 - Accepts: 
  - `obj`: `Object`

This is the same as `Class.extend`, but may be called on any class implementation created using igneous.

## Inspired By:

**Simple JavaScript Inheritance**

By [John Resig][1]

MIT Licensed.

(Inspired by base2 and Prototype)

[Blog Post][2]
 
**TypeScript Enums**

By Microsoft (et al.)

[GitHub Project][3]

[TypeScriptLang.org][4]

## License:

See [LICENSE](https://github.com/JimmyBoh/igneous/blob/master/LICENSE) for more information.

[1]: http://ejohn.org/
[2]: http://ejohn.org/blog/simple-javascript-inheritance/
[3]: https://github.com/Microsoft/TypeScript
[4]: http://www.typescriptlang.org/

---
_Source: https://npm.io/package/igneousjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
