The choice of which of those techniques to use is often relative and can be decided by general principles. There are a few exceptions when a mixin would not accomplish the goal and therefore,
map must be used.
- Use a mixin if you only need to modify some of an object's methods.
map if a mixin won't work or if the file is being changed extensively.
- If in doubt, use a mixin.
map is a key used in
requirejs-config.js that can replace one file with another. This overrides an entire file. Themes have the capability of doing this without
General Principles / Best Practice
Now, what general principles should we use when making the decision of which approach to use? Relative to this question, I suggest the following:
The goal in making modifications is to affect as little code as possible.
This isn't really the only one, though. Other considerations include overall development time, simplicity of the code in question, and probably more.
The benefit of affecting as little code as possible is upgradability and maintainability. If the core file changes, there is a smaller chance of it breaking your update. In addition, since there is less code there altogether, bug fixes or changes to it should be easier.
Specific to your question:
A mixin is a great choice because it affects very little code and is easy to implement. If you were to use
map, you would have to copy and override the entire file. You can't extend the original file like you would with PHP because referencing the original file would load your own file.
With regards to your mixin question:
The code in your example is correct: Magento merges your object over the top of the original one. This replaces the original
initialize method with your method but keeps the rest of the object intact.