Git Product home page Git Product logo

jelly.js's Introduction

jelly.js

jelly.js is a tiny library for module management.

It extends module pattern so that you can improve reusability of modules.

jelly.js provides following functions.

jelly.module(name, fn)

jelly.module defines a module.

Modules is singleton objects which lifecycle is managed by jelly.js.

The first arguemnt is the name of a module. The second argument is a function which defines a module.

You can define a module as following.

jelly.module('Hello', function (def) {
    //This defines private variable
    var defaultName = 'World';

    function getName(name) {
        if (name) {
            return name;
        } else {
            return defaultName;
        }
    }

    //This defines public function
    def.sayHelloTo= function (name) {
        return 'Hello,' + getName(name) + '!';
    };
});

You can use the module as following.

var hello = jelly.module('Hello');
console.log(hello.sayHelloTo('jelly')); //prints Hello, Jelly! 

In the first time the module is used, The function specified as second argument is called with the definition object. You can define public members of the module by add properties to the definition object.

jelly.trait(name, fn)

jelly.module function defines a trait.

A trait is common properties which can be added to other objects.

The first arguemnt is the name of a module. The second argument is a function which defines the trait.

You can define a trait as following.

jelly.trait('Hello', function (def) {
    var name;

    def.setName = function (n) {
        name = n;
    }

    //public methods
    def.sayHello = function () {
        return 'Hello,' + getName(name) + '!';
    };
});

jelly.include(target, trait)

Using jelly.include , you can mix-in properties defined by traits to any objects.

The first argument is a object to add properties defined by traits. The second argument is the name of a trait.

The below example shows the way to create new instance of trait defined above.

var hello = jelly.include({}, 'Hello');
hello.setName('jelly');

jelly.plugin(target, fn)

jelly.plugin defines a plugin. Plugins modify properties of modules or traits.

The first argument is the name of a trait or module to modify. The second argument is a function which modify properties of the target module or trait.

The second argument function is called just after an instance of the target module or trait is created.

Please note that a plugin addition doesn't not affect existing instances of modules or traits.

jelly.reset()

jelly.reset remove all instances of modules.

jelly.js's People

Contributors

nosenaoki avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.