简体   繁体   中英

Manipulating variables in external functions and returning them (JS)

Simple example:

function getSongs() {
  let title = '';
  let name = '';

  getTitle(title);
  getName(name);

  console.log(title, name)
}

function getTitle(title) {
  title = 'Welcome Back';
  return title;
}

function getName(name) {
  name = 'The Black Crusaders'
  return name;
}

getSongs();

I'm trying to do this with several external API's (getting data from the API's in external functions and using that data in the original function), but for the sake of simplicity, above example will do.

Preferably, it would be even better if I didn't have to initialize the variables (title, name) in the first function, but I guess I have to do to this, due to function scope?

I don't know how to do this. Any help is appreciated.

Perhaps something more simple:

function getSongs() {
  var title = getTitle();
  var name = getName();
  console.log(title, name)
}

function getTitle() {
  return 'Welcome Back';
}

function getName() {
  return 'The Black Crusaders'
}

getSongs();

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