viral

As simple as JS OO can be; in a tiny package

Viral

Viral is a tiny, pure prototypal OO library for javascript; taking the best parts of boo.

The most consistent, easiest way to OO in javascript is pure prototypally - and Viral makes this a snap (and packs a tiny punch in the process - < 0.5kb minified).

Viral is a simple base object with two methods:

.extend creates an object that inherits from the object on which it's called, and copies any properties passed to .extend into that new object:

var Person = Viral.extend({
initfunction(firstNamelastName){
this.firstName = firstName
this.lastName = lastName
},
fullNamefunction(){ return this.firstName + this.lastName }
})
 
// extend is inherited by Person, so we can extend further: 
var Coder = Person.extend({
likesCode: true
})

Because Viral uses pure prototypal inheritance - objects inheriting directly from objects - Coder and Person are just objects like any other:

console.log(Person)
 
//  { 
//   init: [Function], 
//   fullName: [Function] 
//  } 
 
console.log(Coder)
 
//  { 
//   init: [Function], 
//   fullName: [Function], 
//   likesCode: true 
//  } 

.make creates an object that inherits from the object on which it's called, and calls the init method of this new object with any arguments you pass in.

// using Coder from the above example: 
var hugh = Coder.make('hugh', 'jackson')
 
hugh.fullName() //= 'hugh jackson' 
hugh.likesCode  //= true 

npm install viral, then require:

var Viral = require('viral')
 
// use `Viral` here 

include as a script tag:

<!doctype html>
<html>
<head></head>
<body>
<script src="libs/viral.min.js"></script> 
<script>
// use `Viral` here 
</script> 
</body>
</html>

include as a script. e.g., from the libs/ folder:

require(['libs/viral.min'], function(Viral){
// use `Viral` here 
})