简体   繁体   中英

Is it possible to get result variable name inside function?

I want to get variable name( user_name1 or user_name2 from sample) which is get result of a function( GetUserName() ) inside this function( GetUserName() ). I need this name for create object with the same name on server side(data synchronization).

function GetUserName(){
  let result_variable_name = ????; //in (1) result_variable_name == 'user_name1', in (2) result_variable_name == 'user_name2'
  socket.send("object_name", result_variable_name);
  return "Some user name";
}
let user_name1 = GetUserName(); //(1)
let user_name2 = GetUserName(); //(2)

Is it possible? How?

No. Just no. Variable names should have no intrinsic meaning. They're just placeholders in an algorithm, and sometimes you can't even control their names to the extend you need. Also, the variable doesn't really exist yet at the time you call the function. And even if it did, there's no sane way to figure out what variable the result of the function will be assigned to from within said function.

Also:

let users = [GetUserName(), GetUserName()];

Now what?

Pass the name explicitly into the function, period.

GetUserName('user_name1')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM