November 1, 2016 by Christoff Truter JavaScript Angular
A few months ago I attempted to join the Borg collective, unfortunately things didn't quite work out as planned.
I initially experienced a bit of a culture shock (individuality, opinion is irrelevant, they only desire mindless drones - one mind, we are as one), their nanoprobes also proved to be incompatible with my physiology (not to mention the polymer nanocomposites used in their uniforms that gave me an unearthly rash), ultimately assimilation failed, so I was forced back into fluidic space.
"Forgive me: the Borg do not evolve, they conquer." - Lieutenant Commander Data
Back in fluidic space I was tasked to optimize the source code used in one of our bio-ships.
This specific class of Fluidian bio-ships currently makes use of AngularJS 1.5.x.
The previous guys (girls? not even sure if we have genders - at this point I am too afraid to ask), used some rather interesting "techniques", one of which included the use of jQuery, hmmm.
Now as a general rule of thumb, I avoid using jQuery in my Angular projects (read a little bit more about it over here).
More specifically they made use of the $.extend utility function, "equivalent" functionality in Angular can be found somewhere between the angular.extend and angular.merge functions (depending on your needs).
There is however one interesting caveat with regards to $.extend, have a look at the following objects in the snippet below.
var x = { a: 1, b: 2, c: 3, d: { e: 4, f: 5 }, o: { p: 6} };
var y = { b: null, d: undefined, o: { q: 7} };
{ a: 1, b: null, c: 3, d: { e: 4, f: 5}, o: { q: 7} }
{ a:1, b:null, c:3, d:undefined, o: { q: 7 } }
Excluding certain properties for certain scenario specific reasons, should really just be up to the developer.
But, lets think about this for a second, Angular and jQuery provides us with a very basic set of utility functions as it is, it is hardly part of their speciality, should we really be using the utility function provided by these libraries?
I always find myself needing more "power" (limited by the technology of my time), which boils down to writing custom utility functions.
Why not rather include a comprehensive specialist library into the equation? (especially if you're only including jQuery for its utility functions - like the guys back at space port) One you can reuse across your various applications, independent of any framework?
Up until recently I preferred using underscorejs as my utility library, but recently I migrated to its more evolved cousin, Lodash (currently maintained by the guys the brought you underscore as well).
Lodash is part of the JS Foundation, which features popular libraries like jQuery, dojo, grunt, moment, RequireJS etc.
If we were to write code that behaves like $.extend using Lodash, it would look something like this (guys familiar with underscore will immediately recognize this):
_.extendWith(x, y, function(target, source) { return _.isUndefined(source) ? target : source; });
Additional Reading
Lodash Homepage
Lodash Wikipedia Link
Only you November 8, 2016 by Jonathan McCarthy
I see what you did there lol!!