Coder Social home page Coder Social logo

configurable.js's Introduction

Configurable

Make a function configurable

Configurable functions modify their configuration. The configuration is available from the function context this

function bar() {
    console.log('foo value is', this.config.foo);
}

bar.bind({ config: { foo: 1 } })(); // 'foo value is 1'
config.foo = 3;
bar.bind({ config: { foo: 3 } })(); // 'foo value is 3'

The configurable behavior modifies the function object, adding one method for each item in the configuration.

The added methods use the same name as the configuration items. The added methods are both setters and getters, which means that they return the config value when called without argument, and they modify the config value when called with an argument.

Additionally each config change create a new configured function. This way it is easy to have several copy of the same function with different configuration.

bar1 = configurable(bar, { foo: 1 });

// Now bar has a foo() method
bar1.foo(); // 1
bar1(); // 'foo value is 1'

var bar2 = bar1.foo(2)
bar2(); // 'foo value is 2'
bar2.foo(); // 2

// but bar1 stay the same
bar1.foo(); // 1
bar1(); // 'foo value is 1'

You can also configure a literal of function, this allow for several function to share the same config.

var foo = {
    logBar: function bar() {
        console.log('bar value is', this.config.bar);
    }
    logRab: function barLength() {
        console.log('reversed bar value is', this.config.bar.split('').reverse().join(''));
    }
}

var foo1 = configurable(foo, { bar: 'hello' });
foo1.logBar(); // bar value is hello
foo1.logRab(); // reversed bar value is olleh

var foo2 = foo1.foo()
foo2.logBar(); // 'bar value is bye'
foo2.logRab(); // 'reversed bar value is yeb'

configurable.js's People

Contributors

djhi avatar fzaninotto avatar jpetitcolas avatar osadan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

osadan

configurable.js's Issues

License?

Since this component is included in eventdrops, could you add a license to package.json and/or LICENSE to the repo?

Thanks!

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.