开发者

Interface Segregation Principle in jQuery

开发者 https://www.devze.com 2023-02-18 02:50 出处:网络
Anyone a开发者_如何学运维ble to give a good illustration of how this works in jQuery? Specifically with regards to the answer from here.

Anyone a开发者_如何学运维ble 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();
})
0

精彩评论

暂无评论...
验证码 换一张
取 消