If you need access to the original function before I alias it, you should instruct to have yours placed above it.
var TH = TH || {};(function ($) { //Added a new plugin to the namespace $.plugin = { newFunc: function(){ //stuff } }; //Doesn't show up in our object testFunction = function(){ //stuff } $.myFunc = function () { // something };})(TH);console.log(TH);//Added a new plugin to the namespaceTH.Plugin2 = {};console.log(TH);This is rather interesting, cause this function and the namespace declaration are very clear.
But what I do like is the fact that, any function not declared with the alias ($) for our namespace doesn't show up in the function isn't added to the object.
And we can still extend the namespace with an object literal.
I prefer to blackbox my code, I think its safer that way
@DarknessFalls: Only if you plan to release your plugin in complete isolation from everyone else's. There are many instances where black boxing is ok, but it's not always the case.
Same here, and now that I know the workings of MV better I intend to improve my plugins on that front too, making them more encapsulated. Yet it's still beneficial to make some functions readily available in a namespace for other developers, for them to add onto or make compatibilities on their end. MV isn't really a framework because its individual parts are too tightly bound together, often in very non-modular ways. In these instances it's important not to make your plugin completely encapsulated.
In general, i try to keep everything encapsulated inside my own plugin. The only stuff i make public are:
- classes, especially for visual stuff (sprites, windows, scenes), so it's easy to modify the visual aspects of my plugins.
- API calls (obviously). Most of these are also callable via plugin commands.
- Some select functions/objects. For example, in my achievement plugin, there are 3 built-in types of achievements (manually triggered, by switch and by variable). Those classes are registered in a "mapping" object and i made it public, so it's easy to extend the plugin with additional achievements, if needed.
In the event that someone needs to get the original function before it was aliased, couldn't they copy+paste the original function code to make a new function to use for their own purpose?
That would rarely be a good solution, and in most cases a very problematic thing to do. What if the original function is later changed for some reason? Suddenly the old broken version lives on in potentially many plugins out there. Recipe for disaster in almost all cases. Yet I've had to do it once now for a compatibility fix, but I wish I didn't.
You'll need 2 plugins in this case:
- 1 that runs before the plugin you want to make a compatibility fix for and stores the old version of the function in a public variable.
- 1 that runs afterwards and uses the stored function.
In my case the issue was with the isDatabaseLoaded() function; I need to call it before the Scene_Boot starts, which was too early for some data reading extensions made to it in other popular plugins. Now I could require my plugin to be placed before all other plugins, aliased isDatabaseLoaded() and stored it, and called the alias when I needed to check the status. In this singular instance I went with duplicating the function because it's a very tiny and self-contained function which in the off-chance that the original is changed, I would need to update other areas of my plugin regardless because it would mean a core change in the core boot and load structure. So I went with the more user-friendly route. But I might change this later on, though, and go for another solution entirely for my PreloadManager.
I think the style I'll stick with is the parameter passing anonymous function.
But include an object for old aliases outside my function, or include a space for aliased functions in the my personal namespace.