JavaScript Extend
One of the most important parts of any programming language is the ability to extend objects, creating your own objects and objects that inherit from those objects. In class-based languages like Java or PHP you’ll see the “extends” keyword being used to set up inheritance between different objects.
JavaScript, however, is structured quite a bit different than class-based languages because it is a prototype-based language. For a more detailed look at JavaScript inheritance see this article here. Additionally, opposed to having different types assigned to variables, in JavaScript everything is an object (and, as a result, everything can be extended)
.
In what follows we’ll take a brief look at JavaScript extend functions. Note that this is a little bit different than inheritance. Extend functions in JavaScript could maybe be thought of merging 2 objects, rather than one object inheriting from another. We’ll refer to these as the target object and an extending object. Remember in JavaScript that an object literal is essentially a collection of key/value pairs. Recall too that in JavaScript object keys can also have functions for their values as well. So if a “key” in our extending object already exists on the target object the value for the key in the extending object will overwrite the value for the key in the target object.
The function to extend an object will look something like what is below…
function extend(targetObject, extendingObject){
for(var key in extendingObject) {
targetObject[key] = extendingObject[key];
}
}
Let’s take a look at how this works in practice. Say we have an object called “robot,” that has a couple of properties…
var robot = {
size: 'small',
gas: 100,
mode: 'helpful',
move: function() {
console.log('Walk')
}
};
Now let’s say we want to extend our robot with some “upgrades”. New properties (keys) will be added to the object and existing properties will overwrite the values for the current object. Will give the robot some more gas (overwriting the robot’s current value), and give it a new mode (setting) and give it a rocket launcher,
var upgrades = {
gas: 200,
mode: 'crush kill destroy',
rocketLauncher: function(){
console.log('BOOM');
}
};
Pass this into the extend function…
extend(robot, upgrades);
And now our robot object looks like the following…
var robot = {
size: 'small',
gas: 200,
mode: 'crush kill destroy',
move: function() {
console.log('Walk')
},
rocketLauncher: function(){
console.log('BOOM');
}
};
Now that we’ve set our robot to “crush kill destroy” it’s a good thing we’ve kept the size at “small.” Hopefully it will be easier to take out if it turns on us.
Let’s take a look at the popular Underscore.js libary’s extend function…
_.extend = function(obj) {
each(slice.call(arguments, 1), function(source) {
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
};
Aside from a little bit of conditional checking and variance in how it’s called, the core functional purpose is essentially the same as the function we wrote above.
You’ll often see the extend function used to pass in different options or settings when calling certain jQuery plugins.
$(window).load(function() {
$('.flexslider').flexslider({
animation: "slide",
animationLoop: false,
itemWidth: 210,
itemMargin: 5,
minItems: 2,
maxItems: 4
});
});
Within the plugin there will be some default settings, but an object literal can get passed in when calling the jQuery plugin that will overwrite the default settings by using the jQuery extend function. This is an easy way to implement custom settings for a jQuery plugin.
So as we have seen, extend functions in JavaScript are a common and useful way to merge two objects together for a variety of purposes. Hopefully you’ll be able to use this when writing some of your JavaScript based applications.
JavaScript, jQuery, Underscore.js





0 Responses to JavaScript Extend