Browsing javascript functions and params

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • optimistx

    #1

    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,propert ies 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(listObjec tProp('window.d ocument.locatio n')); // 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
  • Lasse Reichstein Nielsen

    #2
    Re: Browsing javascript functions and params

    optimistx <optimistxXXXX@ hotmail.com> writes:
    [color=blue]
    > 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);[/color]

    you should make o a local variable, i.e.,
    var o = ...
    and you shouldn't use eval. So, drop o.
    [color=blue]
    > for (var x in o){ s=s+typeof o[x]+":"+x+":"+o[x]+"\n";}
    > return s;
    > }
    >
    > alert(listObjec tProp('window.d ocument.locatio n')); // any object name
    > possible as an argument[/color]

    Drop the eval and just pass the object. I.e.,
    listObjectProp( window.document .location)
    [color=blue]
    > We get numbers, strings, booleans, objects and some user defined
    > functions to this list, but not all functions of objects.[/color]

    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)
    [color=blue]
    > How to get functions also?[/color]

    There is no way, short of guessing their names, if they are not
    enumerable.
    [color=blue]
    > And their parameter lists?[/color]

    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.protot ype.toString:
    ---
    An implementation-dependent representation of the function is
    returned. This representation has the syntax of a
    FunctionDeclara tion. 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,ar gs: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.
    [color=blue]
    > Is the only way
    > to make manually list of all those and fetch from that list with a
    > script?[/color]

    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 - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...