9

Can I access the function arguments outside the function?

Here is my code:

function viewmessage(username,name) {
        //alert(name + " : " + username);
        $('#heading').html(name);
        $.get('/notification/viewmessage', {user:username}, function(data) {
            $('#messagesfrom').html(data);
            $('#newmessage').slideDown(200);
        });
    }
alert(name + " : " + username);
4
  • 5
    There is no nice way to do it, because it shouldn't be done. Commented Feb 27, 2012 at 10:58
  • Not sure exactly what your question is here. Javascript is like most languages, you can't access variables outside their scope. What is it that you are trying to do (more code may help). Commented Feb 27, 2012 at 10:59
  • 1
    That is not possible no. In the meantime, those parameters must come from somewhere right ? How do you call this function ? Why do you want to alert outside the function ? Commented Feb 27, 2012 at 11:00
  • What is your primary motivation to do it? The alert line in your example could be easily done inside the function. Commented Feb 27, 2012 at 11:00

6 Answers 6

7

You can use RegEx (regular expressions) to get the arguments:

function viewmessage(username, name) {/*...*/}
var args = viewmessage.toSource()
           .match(/\((?:.+(?=\s*\))|)/)[0]
           .slice(1).split(/\s*,\s*/g);
//args = ["username", "name"]

Make sure though, you don't have any spaces after the ( or before the ). Otherwise, you might have this result:

function viewmessage( username, name ) {/*...*/}
var args = viewmessage.toSource()
           .match(/\((?:.+(?=\s*\))|)/)[0]
           .slice(1).split(/\s*,\s*/g);
//args = [" username", "name "]

Or you use trim() on each of the arguments after you gathered them:

args.forEach(function (e, i, a) {a[i] = e.trim();});
//args = ["username", "name"]
Sign up to request clarification or add additional context in comments.

Comments

6

You can't, unless you declare the variable outside the function.

You can only use the same variable names in the global scope:

function viewmessage(username, name){
    window.username = username;
    window.name = name;
}
alert(window.name + " : " + window.username ); // "undefined : undefined"
alert(name+" : "+username); // ReferenceError: 'name' not defined

In a local scope, you have to use variable names which are re-declared inside the function:

var username2, name2;
function viewmessage(username, name){
    username2 = username; // No "var"!!
    name2 = name;
}
alert(username2 + " : " + name2); // "undefined : undefined"
viewmessage('test', 'test2');
alert(username2 + " : " + name2); // "test : test2"

1 Comment

@NickyDeMaeyer You're referring to the use of global variables. That's not recommended, because of potential variable conflicts with other embedded scripts. Also, the global scope should not unnecessarily be polluted with unnecessary global variables.
1

You can return those variables in the function:

var viewmessage = function(username, name){
    // Blabla
    return {
        username: username,
        name: name
    }
}

// And then...
var vm = viewmessage('wutup', 'Peter');
alert(vm.name +" : "+ vm.username);

Comments

1

Inside the function me put an the username as id for a submit button. or can use an empty div too.

function viewmessage(username,name){
        //alert(name+" : "+username);
        $("#submit").attr('id', username);
        $('#heading').html(name);
        touser = username;
        $.get('/notification/viewmessage',{user:username},function(data){
            $('#messagesfrom').html(data);
            $('#newmessage').slideDown(200);
        });
    }
var touser = $("#submit").attr("id");
alert(touser);

thanks to those who answered my question.

Comments

0

You don't access the function's parameters from outside the function. That is what variable scoping is all about.

If what you want is to access the variable's value, you need to pass that value to a global variable from inside your function, and then you'll be able to access that value from outside.

Do not confuse the variable with the value.

Comments

0

This relates to the scoping of arguments and where they are defined. You need to wrap the alert in a function that receives the arguments. Either a function that calls viewMessage and then alert, or if you want the alert after the ajax request move the alert into the success handler, as shown:

function viewmessage(username,name){
        $('#heading').html(name);
        $.get('/notification/viewmessage',{user:username},function(data){
            $('#messagesfrom').html(data);
            $('#newmessage').slideDown(200);
            alert(name+" : "+username);
        });
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.