470,644 Members | 1,235 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,644 developers. It's quick & easy.

Browsing javascript functions and params

How to build or find an object browser for javascript?

E.g. Delphi integrated developement environment ('ide') offers a very
practical object browser for Pascal language. When typing a name with a
dot you get a list of objects,properties and methods to select from.
When you select or type a methodname or function and the left
parenthesis , the system writes the possible argument types. With key
F1 one gets thorough description of the object, property, method.

In javascript it is easy to get a list of object properties and its
contained objects t e.g. with this code:

function listObjectProp(obj){
var s='';
o=eval(obj);
for (var x in o){ s=s+typeof o[x]+":"+x+":"+o[x]+"\n";}
return s;
}

alert(listObjectProp('window.document.location')); // any object name
possible as an argument

We get numbers, strings, booleans, objects and some user defined
functions to this list, but not all functions of objects.

How to get functions also? And their parameter lists? Is the only way to
make manually list of all those and fetch from that list with a script?

Venkman and Dom browser in Mozilla might be a way to go but they look a
bit heavy, when here a major part of the work might be done on those 6
code lines above :).

If using mailing address, remove XXXX
Jul 20 '05 #1
1 1486
optimistx <op***********@hotmail.com> writes:
In javascript it is easy to get a list of object properties and its
contained objects t e.g. with this code:

function listObjectProp(obj){
var s='';
o=eval(obj);
you should make o a local variable, i.e.,
var o = ...
and you shouldn't use eval. So, drop o.
for (var x in o){ s=s+typeof o[x]+":"+x+":"+o[x]+"\n";}
return s;
}

alert(listObjectProp('window.document.location')); // any object name
possible as an argument
Drop the eval and just pass the object. I.e.,
listObjectProp(window.document.location)
We get numbers, strings, booleans, objects and some user defined
functions to this list, but not all functions of objects.
That depends on the browser. E.g., Mozilla enumerates most properties,
Opera almost none. The ones that are not enumerated are mostly functions,
but doesn't hgave to be.

Each property of an object is either enumerated or not. The properties
you assign are always enumerable, but the ones provided by the system
might or might not be. Different browsers pick different choices where
it isn't specified. Some properties are always non-enumerable, like
the methods of Array.prototype (required to be non-enumerable by the
ECMAScript standard)
How to get functions also?
There is no way, short of guessing their names, if they are not
enumerable.
And their parameter lists?
There is no way that is *guaranteed* to work. You can get the number of
arguments. It is the length property of the function object.
You can try turning the function into a string and parse it as a function
declaration to get the names of the function arguments, but it is not
required to work. ECMA 262 says about Function.prototype.toString:
---
An implementation-dependent representation of the function is
returned. This representation has the syntax of a
FunctionDeclaration. Note in particular that the use and placement of
white space, line terminators, and semicolons within the
representation string is implementation-dependent.
---
It doesn't say that it has to preserve the names of the variables, although
it would be silly not to.

An example function that gets information out of a function:
---
function parseFunc(func) {
var str = func.toString();
// remove comments to EOL
str = str.replace(/\/\/.*\n/g,"\n");
// remove inline comments
str = str.replace(/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,"");

var arity = func.length;
var match = /function\s*([a-zA-Z_$][\w$]*)?\s*\(([^)]*)\)/.exec(str);
var vars = match[2].split(/\s*,\s*/);
return {name:match[1],arity:arity,args:vars};
}
---
The returned object has the proprties "name" (name of function, if any),
"arity", and "args".

The "name" can be undefined, because current browsers doesn't adhere
to the ECMA 262 standard. They can return a string without a name
for the function, which is then not a function declaration.
Is the only way
to make manually list of all those and fetch from that list with a
script?


For the standard properties which are not enumerated, yes. There is no
other way to find them than to check for them specifically.

For non-standard properties which are not enumerated, there is no way
except shooting in the dark.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by tekenenSPAM | last post: by
1 post views Thread by critchey1 | last post: by
9 posts views Thread by Gabriel Rossetti | last post: by
reply views Thread by Gabriel Genellina | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.