Using the Module Pattern for larger projects

前端 未结 3 1398
小蘑菇
小蘑菇 2021-02-18 23:49

I\'m interested in using the Module Pattern to better organize my future projects. Unfortunately, there are only a few brief tutorials and proof-of-concept examples of the Modul

3条回答
  •  失恋的感觉
    2021-02-19 00:17

    Dojo's dojo.declare is great for this kind of thing since it

    Create a constructor using a compact notation for inheritance and prototype extension.

    It's also really convenient if even for just removing this kind of boiler plate:

    var project = window.project || {};
    project.arm = project.arm || {};
    

    If you just want that feature, then you could use dojo.setObject, but of course, writing something to do the same is trivial.

    dojo.setObject("project.arm.object" (function() {
        var privateVar = "Private contents.";
    
        function privateMethod() {
            alert(privateVar);
        }
    
        return {
            method: privateMethod
        };
    }()));
    

    I recently used dojo.declare/dojo.setObject for a large JavaScript project (86 files, 7K+ lines (not counting comments and blank lines)), and it was a breeze to keep everything organized and manageable, especially when you have an inclusion mechanism like dojo.require and dojo.provide.

提交回复
热议问题