Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

avdgaag/oo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OO.js

A few simple helper methods to help you do object-oriented javascript

Author: Arjan van der Gaag
URL: http://avdgaag.github.com/oo
License: MIT license
Version: 0.1.0


Installation

Simple include the script on your page:

<script src="oo.js"></script>

...or require it in your node.js project:

var OO = require('oo.js');

This library is not yet published as an NPM package, but you can download it manually and install it yourself if you want to using npm install. Once I can think of a better name, I might release it as a package.

Usage example

var Parent = (function() {
    function Parent(foo) {
        this.foo = foo;
        this.getFoo = OO.bind(this, this.getFoo);
    }
    Parent.prototype.getFoo = function() {
        return this.foo;
    };
    return Parent;
})();

var Module = {
    getBar: function() {
        return this.bar;
    }
};

var Child = (function() {
    OO.inherits(Child, Parent);
    OO.extends(Child.prototype, Module);
    function Child(foo, bar) {
        Child.__super__.constructor.call(this, foo);
        this.bar = bar;
    }
    Child.prototype.getFoo = function() {
        return Child.__super__.getFoo.call(this) + '!';
    };
})();

Use properties like normal, even those that are set using the parent's constructor function:

var c = new Child('a', 'b');
c.foo; // => 'a'
c.bar; // => 'b'

Note how the child object decorates the parent object:

c.getFoo(); // => 'a!'

Also note that a method can be bound to an object:

c.getFoo.call({ foo: 'c' }); // => 'a!'

Finally, we have mixed in a module, so its properties become available:

c.getBar(); // => 'b'

Change log

See CHANGELOG for release history.

License

See LICENSE for details. Released under the MIT License.

About

A few simple helper methods for writing Object-Oriented javascript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors