patternjavascriptMinor
Correctly dealing with closures and modularity in Javascript
Viewed 0 times
withjavascriptdealingmodularityandcorrectlyclosures
Problem
The code below is designed to deal with this situation:
There is a Battle environment with Characters; these Characters obviously have certain characteristics and abilities, and they need to be able to use Items from an inventory.
I would like as much feedback as possible on the various aspects of the code:
On the other hand, I have some very specific questions:
There is a Battle environment with Characters; these Characters obviously have certain characteristics and abilities, and they need to be able to use Items from an inventory.
I would like as much feedback as possible on the various aspects of the code:
- The approach to the problem: did I make something that was simple, overly complex?
- The cleanliness/readability of the code: is the code readable and understandable? Am I following the best practices? (Spaces/Tabs/Operator alignment/Brackets…)
- Code syntax: am I
returning correctly? Am I missing some tests that I should do?
On the other hand, I have some very specific questions:
- Am I handling closures correctly? My approach has been to try to be as modular as possible, creating one
global objectto hold all my properties, and creating modules inside iifes.
- I had encountered a problem especially when dealing with Constructors inside the closures. Obviously they are not accessible from the outside with the usual syntax, making it impossible for me to create new Objects in different modules. I solved this problem in the
Characterconstructor by actually adding the function to a variable of theBattleglobal object, as I would do for a method. That, however, doesn't feel right.
- In the Items module the goal is to define the various available items in a dictionary-like
objectorarrayto later add to theBattleglobal object. Does this approach make sense? Adding objects to thearraythe way I do, poses a little problem, though, as the variables lose their name.
- That brings me to the way I solved this problem (which perhaps is unnecessary in the first place) in the Character module. Because I want the syntax of the
useItem(item)method to acceptitemas astringof the name of the item, I go a bit around in loops to find out the corresponding object using the arrayfilter()method. Is this the right way of finding what
Solution
As Shmiddty said, you want your
Battle object to be more of a namespace providing access to the modules:var Battle = function () {}
Battle.Character = (function (/* dependencies */) {
// constructor
var Character = function (name) {
this.name = name;
};
// instance method
Character.prototype.useItem = function () {
[...]
};
// private method
var soSomething = function () {
[...]
};
return Character;
}(/* dependencies */));Code Snippets
var Battle = function () {}
Battle.Character = (function (/* dependencies */) {
// constructor
var Character = function (name) {
this.name = name;
};
// instance method
Character.prototype.useItem = function () {
[...]
};
// private method
var soSomething = function () {
[...]
};
return Character;
}(/* dependencies */));Context
StackExchange Code Review Q#28666, answer score: 2
Revisions (0)
No revisions yet.