Question

Anyone able to give a good illustration of how this works in jQuery?

Specifically with regards to the answer from here.

It sounds the same as Single Responsibility Principle (SRP) for OOP? How is it different?

Was it helpful?

Solution

I feel more comfortable with answering my own question now.

With Single Responsibility Principle, I'm thinking mainly of objects created like in a game. That object should be responsible only for itself, in terms of internal functions/methods.

As for Interface Segregation, I'm thinking more of avoiding anonymous function calls, and segregating a large chunk of code... to different named calls. This was well explained by Paul Irish in jQuery Anti-Patterns for performance.

Instead of anonymous functions bound everywhere like

$(function() {
    //do stuff related to setup menu
    //add facebook, twitter widgets
    //more anonymous actions....
}):

Segregation into a well defined interface might look something like the below:

var homepage = (function() {
    function setupMenu() {
        //do stuff related to setup menu
    }
    function setupSocial() {
        //add facebook, twitter widgets
    }
    return {
        setupMenu:setupMenu,
        setupSocial:setupSocial
    }   
})();

$(function() {
    homepage.setupMenu();
    homepage.setupSocial();
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top