viral

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

Usage no npm install needed!

<script type="module">
  import viral from 'https://cdn.skypack.dev/viral';
</script>

README

viral

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

browser support

Why

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).

API

viral exposes a base object to inherit from - viral.Base. It has two methods:

viral.Base.extend

viral.Base.extend creates an object that inherits from viral.Base, and copies any properties passed to .extend into that new object:

var viral = require('viral')

var Person = viral.Base.extend({
    init: function(firstName, lastName){
        this.firstName = firstName
        this.lastName = lastName
    },
    fullName: function(){ return this.firstName + this.lastName }
})

viral.Base.make

viral.base.make creates an object that inherits from viral.Base, and calls the init method of this new object with any arguments you pass in.

// continuing with the Person example from `viral.Base.extend`

var hugh = Person.make('hugh', 'jackson')   // Person inherits .make from viral.Base

hugh.fullName() //= 'hugh jackson'

Install

node

npm install viral, then require:

var viral = require('viral')

// use `viral` here

browser

include as a script tag:

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

requirejs

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

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