Constants and Block Scoped Variables

ES6 introduces the concept of block scoping. Block scoping will be familiar to programmers from other languages like C, Java, or even PHP. In ES5 JavaScript and earlier, vars are scoped to functions, and they can "see" outside their functions to the outer context.

  1. var five = 5;
  2. var threeAlso = three; // error
  3. function scope1() {
  4. var three = 3;
  5. var fiveAlso = five; // == 5
  6. var sevenAlso = seven; // error
  7. }
  8. function scope2() {
  9. var seven = 7;
  10. var fiveAlso = five; // == 5
  11. var threeAlso = three; // error
  12. }

In ES5 functions were essentially containers that could be "seen" out of, butnot into.

In ES6 var still works that way, using functions as containers, but there aretwo new ways to declare variables: const and let.

const and let use { and } blocks as containers, hence "block scope".Block scoping is most useful during loops. Consider the following:

  1. var i;
  2. for (i = 0; i < 10; i += 1) {
  3. var j = i;
  4. let k = i;
  5. }
  6. console.log(j); // 9
  7. console.log(k); // undefined

Despite the introduction of block scoping, functions are still the preferred mechanism for dealing with most loops.

let works like var in the sense that its data is read/write. let is also useful when used in a for loop. For example, without let, the following example would output 5,5,5,5,5:

  1. for(var x=0; x<5; x++) {
  2. setTimeout(()=>console.log(x), 0)
  3. }

However, when using let instead of var, the value would be scoped in a way that people would expect.

  1. for(let x=0; x<5; x++) {
  2. setTimeout(()=>console.log(x), 0)
  3. }

Alternatively, const is read-only. Once const has been assigned, the identifier cannot be reassigned.

For example:

  1. const myName = 'pat';
  2. let yourName = 'jo';
  3. yourName = 'sam'; // assigns
  4. myName = 'jan'; // error

The read-only nature can be demonstrated with any object:

  1. const literal = {};
  2. literal.attribute = 'test'; // fine
  3. literal = []; // error;

However there are two cases where const does not work as you think it should.

  • A const object literal.
  • A const reference to an object.

    Const Object Literal

  1. const person = {
  2. name: 'Tammy'
  3. };
  4. person.name = 'Pushpa'; // OK, name property changed.
  5. person = null; // "TypeError: Assignment to constant variable.

The example above demonstrates that we are able to change the name property of object person, but we are unable to reset the reference person since it has been marked as const.

Const Reference To An Object

Something similar to the above code is using a const reference, below we've switch to using let for the literal object.

  1. let person = {
  2. name: 'Tammy'
  3. };
  4. const p = person;
  5. p.name = 'Pushpa'; // OK, name property changed.
  6. p = null; // "TypeError: Assignment to constant variable.

Take away, marking an object reference const does not make properties inside the object const.

Ref:.

原文: https://angular-2-training-book.rangle.io/handout/features/constants_and_block_scoped_variables.html