Last Updated: February 25, 2016
·
1.561K
· fernandoperigolo

My favorite JS pattern

I think this is not the most faster pattern, but this the most readable and understandable to me.

The following code is self explanatory. I commented it line to line.

To watch this code working, follow http://jsfiddle.net/DknAX/1/ and observe your console.

// All functions have now the compose method
Function.prototype.compose  = function(argFunction) {
    var invokingFunction = this;
    return function() {
        return  invokingFunction.call(this,argFunction.apply(this,arguments));
    }
}

// For things who work as an electronic
var asEletronic = function() {
    // Exclusive eletronic property
    this.on = false;
    // Exclusive eletronic property
    this.voltage = 110;
    // Exclusive eletronic method
    this.switchPower = function() {
        if(this.on === true) {
            this.on === false;
            console.info('Eletronic is OFF');
        } else {
            this.on === true;
            console.info('Eletronic is ON');
        }
    }
}

// For things who work as an sound reproducer
var asSoundReproducer = function() {
    // Exclusive sound reproducer property
    this.mp3Support = true;
    // Exclusive sound reproducer property
    this.volume = 10;
    // Exclusive sound reproducer method
    this.increaseVolume = function() {
        this.volume++;
        console.info('Volume increased to: ' + this.volume);
    }
    this.decreaseVolume = function() {
        this.volume--;
        console.info('Volume decreased to: ' + this.volume);
    }
}

var TV = function() {
    console.info('New TV instantiated');
    // Core TV property
    this.pol = 42;
    // Core TV property
    this.fullHD = true;
}

var MicroSystem = function() {
    console.info('New Micro System instantiated');
    // Core micro system property
    this.station = 102.9;
    // Core micro system method
    this.setStation = function(newStation) {
        this.station = newStation;
        console.info('Micro System are now in station ' + newStation);
    }
}


// Composing a TV
// TV works like a eletronic
TV = TV.compose(asEletronic);
// TV works like a sound reproducer
TV = TV.compose(asSoundReproducer);

// Composing a Micro System
// Micro system works like a eletronic
MicroSystem = MicroSystem.compose(asEletronic);
// Micro system works like a sound reproducer
MicroSystem = MicroSystem.compose(asSoundReproducer);


// A new TV instantiation
var myTV = new TV();

// Logging myTV to see what it have inside
console.log(myTV);


// A new Micro System instantiation
var myMicroSystem = new MicroSystem();

// Logging myMicroSystem to see what it have inside
console.log(myMicroSystem);


// Turn on my TV
myTV.switchPower();
// Increasing TV volume
myTV.increaseVolume();

// Turn on my Mycro System
myMicroSystem.switchPower();
// Logging current station
console.info('Current station: ' + myMicroSystem.station);
// Setting a new station
myMicroSystem.setStation(88.5);
// Logging current station
console.info('Current station: ' + myMicroSystem.station);

2 Responses
Add your response

Similar to the way I work, although I prefer a local variable over "this":

var tv = (function () {
        var tv = {};
        tv.pol = 42;
        tv.fullHD = true;
        return tv;
})();

I think you'd like my functional js library: https://github.com/leecrossley/functional-js complete with multiple function composition e.g. λ.compose(func1, func2, func3);

over 1 year ago ·

Lee, your option to local variable is a personal choice?

I take a look at your library, but i`m not advanced JS coder. When i look to something like your library, i need to take 2 steps back and learn a lot of other thing. Sorry, but i cant see the beatiful of your library now.

I know Curry becouse i`m a big fan of Angus, but a dont understand the Curry principals totally.

You make me read it now http://en.wikipedia.org/wiki/Tacit_programming. This is a area i like a lot. I try to make my work as simple as possible, make it readable. I love to discovery that exists a science behind my ideas.

I start follow you at GitHub to follow your ideas. See you.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside