Why is there a temporary dead zone?

I know what the temporary dead zone ( TDZ ) is, but I cannot understand the purpose of its existence.

Can someone explain to me why it was created?

What is the logic of creating a ReferenceError or SyntaxError instead of returning undefined ?

ReferenceError:

console.log(typeof foo);
let foo;
Run codeHide result

SyntaxError:

let foo;
let foo;
Run codeHide result

Undefined:

console.log(typeof foo);
Run codeHide result
+4
source share
4 answers

It makes sense that the variable exists from the moment it is defined. Due to the fact that variables from the outer scope are available within the nested scope, the following code is very confusing:

var foo = 'out';

function bar () {
  foo = 'in';
  console.log(foo);
  var foo;
}

bar();

bar()? foo 'in' . - 'out'. . , , , - , .

JS , var, , . :

function setGameMode (mode) {
  if (mode === 'peaceful') {
    var count = 0;
    for (var i = 0; i < mobs.length; ++i) {
      if (mobs[i] instanceOf EvilMob) {
        mobs[i].despawn();
        ++count;
      }
    }
    console.log('Removed ' + count+ ' evil mobs out of ' + i);
    mobSpawner.evil = false;

  } else if (mode ==='chaotic') {
    var count = 0;
    for (var i = 0; i < mobs.length; ++i) {
      if (mobs[i] instanceOf NiceMob) {
          mobs[i].despawn();
          ++count;
      }
    }
    console.log('Removed ' + count + ' nice mobs out of ' + i);
    mobSpawner.nice = false;
  }
}

i - for . var . , var . let "" , , .

, , . , i , if. , JS , . let.

typeof someundefinedvar, 'undefined', , , . , . let .

+2

. , . var, undefined, , , const .

+2
  • console.log(typeof a); let a;

a, . java script . . undefined.

  1. let a; let a;

2- . js - .

  1. console.log(typeof foo);

    , , . undefined. undefined.

0

, , , , , .

: , EMCAScript 3. var . var let , , , , var . . 1999 !

, . , :

// example A
(function () {
console.log(somethingUndefined - 1);
var somethingUndefined;
console.log('another operation');
})();
Hide result

, . ?

// example B
(function () {
try {
    console.log(somethingUndefined - 1);
    var somethingUndefined = 50;
    console.log('another operation');
} catch (e) {
    console.log('I want to deal with problems here');
    return;
}

console.log('plowing on');
})();
Hide result

.

:

// example C
(function () {
var somethingUndefined = undefined;
try {
    console.log(somethingUndefined - 1);
    somethingUndefined = 50;
    console.log('another operation');
} catch (e) {
    console.log('I want to deal with problems here');
    return;
}

console.log('plowing on');
})();
Hide result

" ", B C , somethingUndefined -. "undefined", somethingUndefined === undefined, . 50, , . , , -, . 1, 8, . :

// example D
(function () {
try {
    console.log(somethingUndeclared - 1);
    console.log('another operation');
} catch (e) {
    console.log('I want to deal with problems here');
    console.log(e);
}
})();
Hide result

D somethingUndeclared . . , EMCAScript , , .

- somethingUndeclared, , . , , var somethingUndeclared; - - , anoy . let const .

let const, " ", , -, -. undefined, . let, , , var . var .

// example D
(function () { // temporal dead zone, something is nothing
try { // temporal dead zone, something is nothing
    console.log(something - 1); // exceptional behavior! temporal dead zone
    let something  = 50; // temporal live zone begins here!
    console.log('another operation');
} catch (e) {
    console.log('I want to deal with problems here'); // you could call this a dead zone
    console.log(e); // dead
}
// dead
})(); // way dead
Hide result

: " , ?" , , , . javascript, , UI. , EMCAScript . . , , , . , , ('99).

When we need this to do what he needs to do all the time, we can use these functions. When it would be a disaster undefined = 100;to be in some kind of file in the project, we can refuse exceptions. When a progressive improvement fails and we don't get any tooltips, we have a less good UX than we hoped. var and let them have different stories and different weights on their shoulders, so they are likely to always act differently.

0
source

Source: https://habr.com/ru/post/1671989/


All Articles