Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Function | Q800 [ previous next ]

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

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>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.