Parametre for ytre og indre funksjoner i JavaScript

La oss gjøre det slik at både den ytre funksjonen og den indre tar imot parametre:

javascriptfunction test(num) {
  function func(localNum) {
    console.log(localNum);
  }
}

La oss sende parameteren til den ytre funksjonen i kallet til den indre funksjonen:

javascriptfunction test(num) {
  function func(localNum) {
    console.log(localNum); // vil skrive ut 1
  }
  
  func(num); //!! sender parameteren
}

test(1); // sender et tall som parameter

Resultatet er at i den indre funksjonen vil variabelen num være tilgjengelig som en ekstern variabel fra forelderfunksjonen, og variabelen localNum, som er en lokal variabel i den indre funksjonen.

Begge disse variablene vil ha samme verdi:

javascriptfunction test(num) {
  function func(localNum) {
    console.log(num); // vil skrive ut 1
    console.log(localNum); // vil skrive ut 1
  }
  
  func(num);
}

test(1);

Forskjellen mellom dem vil være følgende: hvis variabelen num endres i den indre funksjonen, vil den også endres i den ytre funksjonen:

javascriptfunction test(num) {
  function func(localNum) {
    num = 2; // endrer variabelen num
  }
  
  func(num);   // sender parameteren
  console.log(num);  // vil skrive ut 2
}

test(1); // sender et tall som parameter

Mens variabelen localNum vil være lokal. Endringer i den vil ikke føre til noen endringer i den ytre funksjonen. Og selve variabelen localNum vil ikke være synlig utenfor den indre funksjonen:

javascriptfunction test(num) {
  function func(localNum) {
    localNum = 2; // endrer variabelen num
  }
  
  func(num); // sender parameteren
}

test(1); // sender et tall som parameter
Задание 1

Bestem, uten å kjøre koden, hva som vil skrives ut i konsollen:

javascriptfunction test(num) {
  function func(localNum) {
    console.log(localNum);
  }
  
  func(num);
}

test(1);
Задание 2

Bestem, uten å kjøre koden, hva som vil skrives ut i konsollen:

javascriptfunction test(num) {
  function func(localNum) {
    console.log(localNum);
  }
  
  func(num + 1);
}

test(1);
Задание 3

Bestem, uten å kjøre koden, hva som vil skrives ut i konsollen:

javascriptfunction test(num) {
  function func(localNum) {
    console.log(num);
  }
  
  func(num + 1);
}

test(1);
Задание 4

Bestem, uten å kjøre koden, hva som vil skrives ut i konsollen:

javascriptfunction test(num) {
  function func(localNum) {
    localNum = 2;
  }
  
  func(num);
  console.log(num);
}

test(1);
Задание 5

Bestem, uten å kjøre koden, hva som vil skrives ut i konsollen:

javascriptfunction test(num) {
  function func(localNum) {
    localNum = 2;
  }
  
  func(num);
  console.log(localNum);
}

test(1);
Задание 6

Bestem, uten å kjøre koden, hva som vil skrives ut i konsollen:

javascriptfunction test(num) {
  function func(localNum) {
    num = 2;
  }
  
  func(num);
  console.log(num);
}

test(1);