문제

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?

도움이 되었습니까?

해결책

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();
})
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top