Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm refactoring an in house framework from using nested namespaces towards using amd (require.js).

In the 'old' version I have 2 files.

Core.js
Core.Window.js

Both of them contain utitily functions, but only Core.js can be used in WebWorkers. Window.js accesses the DOM.

Let's say I Core.js looks like this;

JavaScript
define([], function () {

    //This function should be there for both workers and window alike
    function specialfunction() {
        return "I'm special";
    }
    return {
        specialfunction: specialfunction
    }

});

And then in Core.Window I create a dependency. But I extend the core object;

JavaScript
define(["core"], function (core) {

    core.doSomethingDommy = function (id) {
        return document.getElementById(id);
    }

    return core;

});


So now in the normal window scope I can define "core.window" and in the worker scope I can define "core". I end up with a familair object to other implementors in both case, e.g. core.

Is this in anyway considered bad practice?
Posted
Comments
Sergey Alexandrovich Kryukov 7-May-15 14:10pm    
Hard to see what is it, as you did not show the definition of define or at least the signature and explanation of semantics. But why would you care about "considered" practices if your practice might be better? Use only the pure logic, consider possible consequences on maintainability, possibilities of misuse, and so on. From the first glance, you are not doing anything special. You are adding a function property to a core through a function parameter, so what? After all, we often modify by-reference method parameters...

Breaking a pattern? What pattern? After all, remember that patterns are used to help some project development, not that the projects are developed to support patterns.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900