Skip to main content
clarified "external" since such a function declared internally could still work
Source Link
durette
  • 394
  • 1
  • 12

Writing aan external function to do this would be impossible, and I'll demonstrate that with a proof by contradiction.

Let's assume we've written our magic function called getWhatsInScope. Now, let's say we want to call it from our function myFunction and see if it picks up our function variable myVariable. This is a contradiction because no matter how we write getWhatsInScope, it can't see myVariable.

function getWhatsInScope () {
  console.log(myVariable) // out of scope
}

function myFunction() {
  const myVariable = 42
  getWhatsInScope()
}

Writing a function to do this would be impossible, and I'll demonstrate that with a proof by contradiction.

Let's assume we've written our magic function called getWhatsInScope. Now, let's say we want to call it from our function myFunction and see if it picks up our function variable myVariable. This is a contradiction because no matter how we write getWhatsInScope, it can't see myVariable.

function getWhatsInScope () {
  console.log(myVariable) // out of scope
}

function myFunction() {
  const myVariable = 42
  getWhatsInScope()
}

Writing an external function to do this would be impossible, and I'll demonstrate that with a proof by contradiction.

Let's assume we've written our magic function called getWhatsInScope. Now, let's say we want to call it from our function myFunction and see if it picks up our function variable myVariable. This is a contradiction because no matter how we write getWhatsInScope, it can't see myVariable.

function getWhatsInScope () {
  console.log(myVariable) // out of scope
}

function myFunction() {
  const myVariable = 42
  getWhatsInScope()
}
Source Link
durette
  • 394
  • 1
  • 12

Writing a function to do this would be impossible, and I'll demonstrate that with a proof by contradiction.

Let's assume we've written our magic function called getWhatsInScope. Now, let's say we want to call it from our function myFunction and see if it picks up our function variable myVariable. This is a contradiction because no matter how we write getWhatsInScope, it can't see myVariable.

function getWhatsInScope () {
  console.log(myVariable) // out of scope
}

function myFunction() {
  const myVariable = 42
  getWhatsInScope()
}