- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Actually, it's even better. She just discovered the joys of dynamic JavaScript object methods.
Huh?
In the most popular programming languages, the methods that a class has are defined as part of the class, and they're fixed — you can't change them while the program is running. These are static object methods.
Most of the time when you're using JavaScript, that's probably how you think about JavaScript as well. But JavaScript actually has dynamic object methods that you can change while the program is running. If you've never seen anything like this before, even the idea of dynamic methods may seem strange.
Here's a very simple example just to show you how the mechanics work. This isn't intended to be realistic or useful:
// define a class X - with no methods...
function X(a, b) {
this.x = a;
this.y = b;
}
// make a couple instances of class X...
var one = new X(3, 4);
var two = new X(5, 6);
// make a couple functions...
var mul = function() {
return this.x * this.y;
}
var add = function() {
return this.x + this.y;
}
// dynamically assign methods to our instances of class X!
one.process = mul;
two.process = add;
// now run them to show the results...
gs.log(one.process());
gs.log(two.process());
Let's take this from top to bottom...
First we're defining the class X, which is ridiculously simple. Its constructor just stores the two parameters as instance properties, and there are no instance methods defined.
Next we create two instances of this class, in one and two. Nothing fancy there.
Now we make a couple of functions: mul and add. These look a little odd, mainly because in the body of each of these functions we're using the "this" reference — but these are just standalone functions, not part of any object, so what does the "this" reference actually mean? Actually, these are part of an object (the global object), even though we're not really thinking of them as such. But even so, the "this" reference looks odd here, because we think of it being used when we call a function as a method. If we just called one of these as a standalone function (like mul();, it would return NaN (not a number) because the internal uses of the "this" reference wouldn't return anything sensible. For the moment, just ignore this oddness.
Next comes the part that got Trudy all excited — and which will make sense of the preceding. On each of the instances of the class X we'd previously made, we assign one of the functions we just made to the property process. When a property of an object contains a function, that property becomes a method of that object. So we just manufactured a method (process) on our two instances — a method that we didn't define as part of the class (that is, we didn't assign it to the prototype). That's a dynamic method!
Finally, we call the new process() methods and observe that they actually do what we wanted them to do. Note that because we're now calling them as an object method, the "this" reference makes sense — and returns the values we'd expect it to.
Where is this sort of thing useful? I've used it myself most commonly in circumstances where I have objects with data in them, but I won't know until runtime what I need to do with the data. One way (most certainly not the only way!) to accomplish this is to build objects with that data, then use dynamic methods to actually do something with that data. This is really what the simple example above does. Another place where I've used dynamic methods is when I'm letting the user define some script to do something, and then I (carefully!) eval() that script as a function body, and use the result to populate a dynamic method. I've also seen nifty examples of dynamic methods in several open source JavaScript client libraries. It's one of those generally useful tools that you might not need very often, but when you do need it, you're very glad to have it in your toolbox.
Trudy collects fine tools, and a new one makes her very happy...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.