How can I access an argument to a JavaScript function outside the function?

Can I access 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
source share
6 answers

You cannot, unless you declare a variable outside the function.

You can 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 the local scope, you should use variable names that 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" 
+5
source

You can return these variables to a function:

 var viewmessage = function(username, name){ // Blabla return { username: username, name: name } } // And then... var vm = viewmessage('wutup', 'Peter'); alert(vm.name +" : "+ vm.username); 
+1
source

Inside the function, I put the username as id for the 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.

+1
source

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 that after ( or before ) you have no spaces. Otherwise, you can get 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() for each of the arguments after they are built:

 args.forEach(function (e, i, a) {a[i] = e.trim();}); //args = ["username", "name"] 
+1
source

You do not access function parameters outside the function. This is for the variable.

If you want to access the value of a variable, you need to pass this value of the global variable from within your function, and then you can access this value from the outside.

Do not confuse a variable with a value.

0
source

This refers to defining arguments and defining them. You need to include the alert in a function that receives arguments. Either the function that calls viewMessage and then the warning, or if you want the warning after the ajax request to move the warning to the success handler, as shown below:

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

Source: https://habr.com/ru/post/1398586/


All Articles