JavaScript Object Manipulation
JavaScript is a very dynamic language, so much so that you can redefine and change objects on the fly. This can be a strange concept to programmers from a more traditional OO background such as Java or C#, however once made familiar it can be very useful.
This feature is not restricted to modifying your own objects; you can change the built in JavaScript objects. Take the following example, First a simple demo environment;
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>Object Creation and Manipulation</title>
-
-
</head>
-
<body>
-
-
<div id="mydiv">
-
-
</div>
-
-
<script type="text/javascript" src="js.js"></script>
-
-
</body>
-
</html>
Now a short bit of JavaScript in the js.js file…
-
var div = document.getElementById(‘mydiv’);
-
-
div.hide = function(){
-
-
this.style.display = "none";
-
-
}
-
-
div.hide();
-
-
div.show = function(){
-
-
this.style.display = "";
-
-
}
-
-
div.show();
When the JavaScript file is called it initially retrieves the mydiv object from the DOM tree. Next are the interesting lines; “div.hide = function(){…” assigns a new function object to ‘hide’ on the object. This doesn’t need to be predefined in any particular way. Then inside the curly brackets you can write a function much as you normally would. However, note that the ‘this’ keyword inside the function relates to the object that the function will be inside.
After this function is created the following lines show that is it called just like a normal function. To demonstrate what it is doing it is worth noting that this could be done by doing;
-
var div = document.getElementById(‘mydiv’);
-
div.style.display = "none";
This works quite well, however say you want to add this to a number of Elements if not all? This could be a hassle. Rather than extending that particular object, we extend the object that it derives from. This works with the same HTML.
-
Element.prototype.destroy = function(){
-
-
this.parentNode.removeChild(this);
-
-
}
-
var div = document.getElementById(‘mydiv’);
-
-
alert(div);
-
div.destroy();
-
-
div = document.getElementById(‘mydiv’);
-
alert(div);
The above code adds a function to all elements. This means all the different HTML elements in the page will then have this function available to them. This easily allows you to add useful helper functions that you will re-use on a particular website.
You can change other objects too. Remember in JavaScript Arrays and Functions are objects, so you can augment these in much the same way. For example, the following function gets every second item in an array.
-
Array.prototype.odd = function(){
-
-
var newArray = [];
-
for(var x=0;x<this.length;x=x+2){
-
-
newArray.push(this[x])
-
-
}
-
-
return newArray;
-
-
}
-
-
var test = [1,2,3,4,5,6,7,8];
-
-
alert(test.odd());
Note this example is only tested for FireFox/Opera. IE example to come soon. It should work in Safari, but I’m macless and can’t test.
Tags: augmentation, JavaScript, lambda, syntactic sugar
