Z
Z
zlodiak2020-04-25 12:43:25
JavaScript
zlodiak, 2020-04-25 12:43:25

Is the global object a function too?

There is the following code:

const q = 1;

function outer() {
  console.log('outer run')
  const w = 2;
  
  return function inner() {
  	console.log('inner run')
  	const e = 3;
    console.log(q);
  }
}

outer()()


There are 3 lexical environments here:
inner() -> outer() -> global

The order of their creation is as follows:
1. When inner() is called, a lexical environment (LO) is created for it, which refers to a more external LO (i.e., outer LO)
2. When outer() is called, LO is created for it , which refers to the global LO

The problem is that inner() and outer() are functions, and the global LO is not a function. Why is the global LO also considered by the js engine as a function? Is this an exception or how to understand it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shsv382, 2020-04-25
@zlodiak

In general, in JavaScript, all these are objects. Even functions are objects.

A
Alexander, 2020-04-25
@Seasle

Closure .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question