This package has been deprecated

Author message:

This module is no longer maintained. If you're interested in taking over maintenance of this repo, please contact the author

chic

1.1.0 • Public • Published

Chic

Chic is an extremely simple class-like interface to JavaScript prototypal inheritance.

Current Stable Version: 1.1.0
Automated Build Status: Build Status
Node Support: 0.6, 0.8, 0.10
Browser Support: Android Browser 2.2–4.2, Firefox 3.6, Firefox 4–19, Google Chrome 14–25, Internet Explorer 6–10, Mobile Safari iOS 3–6, Opera 12.10, Safari 5–6

Getting Started

You can use Chic on the server side with Node.js and npm:

$ npm install chic

On the client side, you can either install Chic through Component:

$ component install rowanmanning/chic

or by simply including chic.js in your page:

<script src="path/to/lib/chic.js"></script>

Usage

In Node.js or using Component, you can include Chic in your script by using require:

var Class = require('chic').Class;

Chic also works with AMD-style module loaders, just specify it as a dependency.

If you're just including with a <script>, Class is available in the chic namespace:

var Class = chic.Class;

The rest of the examples assume you've got the Class variable already.

Create a class

Creating classes is very simple. You extend the base class like this:

var Animal = Class.extend();

Obviously you want to add methods to your class, to give it some functionality:

var Animal = Class.extend({
    eat:   function () { ... },
    sleep: function () { ... },
    poop:  function () { ... }
});

The init method is a special one. This is your class constructor, and is called when a new instance of your class is created. You can set things up in here.

var Animal = Class.extend({
    init: function () {
        this.alive = true;
    }
});

Instantiating a class

Instantiating your new class is just like instantiating any other JavaScript class now. You'll be able to use all those methods you defined!

var fluffy = new Animal();
fluffy.poop(); // Bad Fluffy!

Extending classes

Any class you create is also extendable. You extend custom classes in exactly the same way as the base class:

var Cat = Animal.extend();

If you define methods in this extend, then they will override methods of the same name which have been inherited from the parent class. For example:

var Animal = Class.extend({
    speak: function () {
        return 'Roar!';
    }
});
 
var Cat = Animal.extend({
    speak: function () {
        return 'Miaow?';
    }
});
 
var mrTibbles = new Cat();
mrTibbles.speak(); // Miaow?

If you wish to call the parent method, then that's possible using this.sup, which is a reference to the parent method with the same name as the one being called:

var Animal = Class.extend({
    init: function (name) {
        this.name = name;
    },
    eat: function () {
        return this.name + ' is eating';
    }
});
 
var Cat = Animal.extend({
    eat: function () {
        return this.sup() + ' like a good kitty';
    }
});
 
var pet = new Cat('Mr Tibbles');
pet.eat(); // Mr Tibbles is eating like a good kitty

Extending Non-Chic Classes

This feature is planned, and will be introduced in the near future. In the meantime, @jhnns has this functionality working in his fork.

Development

To develop Chic, you'll need to clone the repo and install dependencies with make deps. If you're on Windows, you'll also need to install Make for Windows.

Once you're set up, you can run the following commands:

$ make deps         # Install dependencies 
$ make lint         # Run JSHint with the correct config 
$ make test         # Run unit tests in Node 
$ make test-server  # Run a server for browser unit testing (visit localhost:3000) 

When no build target is specified, make will run deps lint test. This means you can use the following command for brevity:

$ make

Code with lint errors or no/failing tests will not be accepted, please use the build tools outlined above.

Credit

This library was inspired by John Resig's great Simple JavaScript Inheritance post.

License

Chic is licensed under the MIT license.

Package Sidebar

Install

npm i chic

Weekly Downloads

9

Version

1.1.0

License

none

Last publish

Collaborators

  • darkguy2008
  • rowanmanning