开发者

Gloal Abatement - Using a Single Object Literal

So I just need a sanity check on the way in which I layout my code for an application. I'm always keen to learn better approaches.

I basically use an Object Literal to organise my code meaning that I have one single global variable. Then for each section of the application I create a separate object - somethin开发者_如何转开发g like:

var MYAPP = {

        init : function() {
            //site wide common js
        },

        sections : {

            homepage : function() {
                //homepage js
            },

            anotherpage : function() {
                //another page js
                tools.usefultool();
            }

        },

        tools : {

            usefultool : function() {
                //useful reuseable method
            }

        }
};

My question is while this helps code organisation, I'm wondering about objects being initialised but never used. For example - if I'm on a site's homepage I'll just call MYAPP.sections.homepage() . I don't actually need any of the other objects so I'm wondering - does this structure have a performance implication? Is there a better way? The structure closely follows the the great Rebecca Murphy article "Using Object to Organise Your Code" (http://blog.rebeccamurphey.com/2009/10/15/using-objects-to-organize-your-code).

Thanks!


Yes, there's always a performance hit in unused code as the parser has to actually interpret the code even if it's not executed. But any performance hit here is so minute that you're never going to notice it. The only real hit in unused code like this is in the bandwidth required to download it. If you have a 100kb file downloaded that you never use then you're wasting the time to download that file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜