Skip navigation

I actually have done this in JavaScript.

metaprogramming
program that writes code that it will later run (itself)

I guess this is like generating JavaScript from PHP code.

I didn’t realize it was called metaprogramming. But its basically when you use code to write code.

When you start metaprogramming, the application boosts in complexity10 or something. It becomes very hard to understand, and even harder to debug. Because you’ve now got 2 levels to debug on: is the code being generated correctly (php side), and is the code running correctly (javascript side).

I suppose you might use code of one language to run again later from that same language. E.g. writing Python code that creates Python code, which is later evaluated.

Might be useful for generating a callback, or something.

Another form of metaprogramming is these frameworks (prototype, joose) that have facilities to write “Classes” in JavaScript (even though keyword “class” doesn’t exist in JavaScript 1.5). You write something like:

var Animal = Class.create({
initialize: function(name, sound) {
this.name = name;
this.sound = sound;
},

speak: function() {
alert(this.name + ” says: ” + this.sound + “!”);
}
});

Then the prototype framework takes that code you wrote and turns it into “real javascript” that can actually run

function Animal( name, sound )
{
this.name = name ;
this.sound = sound ;

this.speak = function() {
alert(this.name + ” says: ” + this.sound + “!”);
}

return this ;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: