David Cramer's Blog

Python, Django, and Scale.

Dir() in JavaScript

I was working with some JavaScript code today (for our new moderation tools on Curse), and found something interesting. Let me first say by in no way do I claim to be a JavaScript expert, so don’t tease me too much.

JavaScript dir() method.

1
2
3
4
5
6
7
8
9
function dir(object)
{
    methods = [];
    for (z in object) {
        if (typeof(z) != 'number') {
            methods.push(z);
        }
        return methods.join(', ');
    }

This works on certain elements and outputs a list like it would in Python, of all the methods that are attached to it. I added the typeof() clause to stop it from outputting slices.

Maybe there’s a better way to do this (without a debugger), but this was my best guess.

Comments