Socket
Socket
Sign inDemoInstall

hydrogen

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hydrogen

A minimal library for object-oriented JavaScript development


Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

hydrogen

A minimal library for object-oriented JavaScript development.

Build Status

How To Use

Node.js

Add hydrogen to your dependencies, then run:

npm install

Then add:

var h = require('hydrogen');

Browser

<script type="text/javascript" src="path/to/hydrogen.js"></script>

Usage Examples

See sample/index.html for the examples below plus a few more.

Inheritance

h.create() can be used to create a prototypical object, or extend one prototypical object from another.

function Character(name, mediumName, otherName) {
    this.name = name;
    this.mediumName = mediumName;
    this.otherName = otherName;
}
h.create(Character, {
    getDescription: function() {
        return this.getName() + ' is from "' + this.mediumName + '"';
    },
    getName: function() {
        if (this.otherName) {
            return this.name + ' (a.k.a. ' + this.otherName + ')';
        } else {
            return this.name;
        }
    }
});

function Villain(name, mediumName, otherName, wearsMask) {
    this.super.constructor.call(this, name, mediumName, otherName);
    this.wearsMask = wearsMask;
}
h.create(Villain, Character, {
    getDescription: function() {
        return this.getName() + ' is from "' + this.mediumName + '". Mwahahaha!';
    }
});

var mcclane = Character.makeInst('John McClane', 'Die Hard');
mcclane.name; // John McClane
mcclane.getDescription(); // John McClane is from "Die Hard"
var vader = Villain.makeInst('Darth Vader', 'Star Wars', 'Anakin Skywalker', true);
vader.getDescription(); // Darth Vader (a.k.a. Anakin Skywalker) is from "Star Wars". Mwahahaha!

Private Variables

h.attach() adds a closure to an object, which can be used to contain private variables and functions.

function Weapon(name) {
    this.name = name;
}
h.attach(Weapon, function() {
    var users = [];

    return {
        addUser: function(user) {
            users.push(user);
        },
        getUsers: function() {
            return users;
        }
    };
});

var bullwhip = Weapon.makeInst('Bullwhip');
bullwhip.users; // undefined
bullwhip.addUser('Indiana Jones');
bullwhip.addUser('Catwoman');
bullwhip.getUsers(); // Indiana Jones,Catwoman

Properties returned by attached closure can be overridden in a child object via either h.create() or h.attach(). See sample/index.html for examples.

Keywords

FAQs

Package last updated on 15 Jun 2013

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc