Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q800 Is there a way to call on a function without knowing its name?

You are here: irt.org | FAQ | JavaScript | Function | Q800 [ previous next ]

There is a Function object, but it doesn't hold a list of the functions within the current page, but the properties and methods of a function.

For example:

<SCRIPT LANGUAGE="JavaScript"><!--
function myFunctionName() {
    var output = '';
    output += 'arguments.length = ' + arguments.length + ' : ';
    for (var i = 0; i < arguments.length; i++)
        output += arguments[i] + ' ';
    output += '<BR>';
    output += 'myFunctionName.caller = ' + myFunctionName.caller + '<BR>';
    alert('myFunctionName.toString = ' + myFunctionName.toString());
    document.write(output);
}
//--></SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.1"><!--
function myFunctionName(a,b,c) {
    var output = '';
    output += 'arguments.length = ' + arguments.length + ' : ';
    for (var i = 0; i < arguments.length; i++)
        output += arguments[i] + ' ';
    output += '<BR>';
    output += 'myFunctionName.length = ' + myFunctionName.length + ' : ';
    for (var i = 0; i < myFunctionName.length; i++)
        output += arguments[i] + ' ';
    output += '<BR>';
    output += 'myFunctionName.caller = ' + myFunctionName.caller + '<BR>';
    output += 'myFunctionName.prototype = ' + myFunctionName.prototype + '<BR>';
    alert('myFunctionName.toString = ' + myFunctionName.toString());
    document.write(output);
}
//--></SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.2"><!--
function myFunctionName(a,b) {
    var output = '';
    output += 'arguments.length = ' + arguments.length + ' : ';
    for (var i = 0; i < arguments.length; i++)
        output += arguments[i] + ' ';
    output += '<BR>';
    output += 'arguments.caller = ' + arguments.caller + '<BR>';
    alert('arguments.callee = ' + arguments.callee);
    output += 'Name of function = ' + arguments.callee.toString().match(/function (\w*)/)[1] + '<BR>';
    output += 'myFunctionName.length = ' + myFunctionName.length + ' : ';
    for (var i = 0; i < myFunctionName.length; i++)
        output += arguments[i] + ' ';
    output += '<BR>';
    output += 'myFunctionName.arity = ' + myFunctionName.arity + ' : ';
    for (var i = 0; i < myFunctionName.arity; i++)
        output +=arguments[i] + ' ';
    output += '<BR>';
    output += 'myFunctionName.caller = ' + myFunctionName.caller + '<BR>';
    output += 'myFunctionName.prototype = ' + myFunctionName.prototype + '<BR>';
    alert('myFunctionName.toString = ' + myFunctionName.toString());
    document.write(output);
}
//--></SCRIPT>

<SCRIPT LANGUAGE="JavaScript"><!--
myFunctionName('one','two','three','four');
//--></SCRIPT>

Although, as you can see above in JavaScript 1.2 we can extract the name of the function within the function itself. The following demonstrates a generic function that invokes itself:

<SCRIPT LANGUAGE="JavaScript1.2"><!--
function callSelfTwice() {
    if (arguments.caller == null) {
        // call ourselves once more if not called from a function
        alert('Hello');
        eval(arguments.callee.toString().match(/function (\w*)/)[1] + '()');
    }
    else {
        alert('World');
    }
}

callSelfTwice();
//--></SCRIPT>

©2018 Martin Webb