Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
A minimal library for object-oriented JavaScript development.
Add hydrogen
to your dependencies, then run:
npm install
Then add:
var h = require('hydrogen');
<script type="text/javascript" src="path/to/hydrogen.js"></script>
See examples for the ones below plus more.
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!
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 an attached closure can be overridden in a child object via either h.create()
or h.attach()
, see examples.
h.checkImpl()
checks a value against an "interface" and determines if the value is a valid implementation of the interface.
var IMovie = {
type: 'object',
contents: {
title: {
type: 'string'
},
details: {
type: 'object',
contents: {
summary: {type: 'string'},
year: {type: 'number'}
}
},
play: {
type: 'function',
minArity: 2
}
}
};
var minorityReport = {
title: 'Minority Report',
details: {
summary: 'In a future where a special police unit is able to arrest murderers before ' +
'they commit their crimes, an officer from that unit is himself accused of a future murder.',
year: 2002,
cast: [
'Tom Cruise',
'Colin Farrell',
'Samantha Morton',
'Max von Sydow'
]
},
play: function(startTime, fullScreen, showSubtitles) {}
};
var warOfTheWorlds = {
title: 'War of the Worlds',
details: {
summary: 'As Earth is invaded by alien tripod fighting machines, one family fights for survival.',
year: 2005
},
play: function(fullScreen) {}
};
h.checkImpl(minorityReport, IMovie); // true
h.checkImpl(warOfTheWorlds, IMovie); // At key ["play"]: Function does not accept at least 2 parameters
See examples for more.
FAQs
A minimal library for object-oriented JavaScript development
The npm package hydrogen receives a total of 0 weekly downloads. As such, hydrogen popularity was classified as not popular.
We found that hydrogen demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.