Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access JavaScript function argument outside the function?

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);
like image 910
Krishna Raj Avatar asked Feb 27 '12 10:02

Krishna Raj


People also ask

How do you access a variable outside a function in JavaScript?

How do you call a variable inside a function in JavaScript? So the easiest way to make your variable accessible from outside the function is to first declare outside the function, then use it inside the function. var a; Parse.

How do you call a function outside in JavaScript?

var camera = (function () { function a(){ console. log('calling ..a'); } function b(){ console. log('calling .. b'); } })(); //calling function a() from outside camera.

How do you pass an argument from one function to another in JavaScript?

Arguments are Passed by Value The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.

How do you access the arguments of an object in a function?

The arguments object is a local variable available within all non-arrow functions. You can refer to a function's arguments inside that function by using its arguments object. It has entries for each argument the function was called with, with the first entry's index at 0 . You can use arguments.


2 Answers

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"
like image 70
Rob W Avatar answered Sep 27 '22 20:09

Rob W


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"]
like image 22
jaySon Avatar answered Sep 27 '22 18:09

jaySon