Skip to content

Latest commit

 

History

History
370 lines (246 loc) · 4.16 KB

book2-chap2-quiz.answers.md

File metadata and controls

370 lines (246 loc) · 4.16 KB

Quiz - YDKJS: Scope & Closures 2/5

Chapter 2. Lexical Scope

Answer Sheet

Section: Lex-time


1. What are the two predominant models for how Scope works used by the majority of programming languages?

lexical scope

dynamic scope

2. Which is the Scope model that JavaScript employs?

lexical scope

3. How many Scopes can you identify in the following snippet, and what would be the output?
Snippet #1
var result = (function (a) {
  function foo(b) {
    return bar(5);

    function bar(c) {
      return a + b + c;
    }
  }

  return foo(2);
})(3);

console.log(result);

There are 4 Scopes:

global scope

- anonymous function (iife) scope

-- foo scope

--- bar scope

Output: 10

4. What is shadowing?

when an identifier of an inner scope shadows an identifier of an outer scope.

5. Look Up in nested scopes. What would be the output for the following snippets?

NOTE: replace window with global if you are using node.js

Snippet #2
var name = "John";

function foo(name) {
  function bar() {
    var name = "Doe";

    return (function baz() {
      return name;
    })();
  }
  return bar();
}

console.log(foo(name));

"Doe"

Snippet #3
var a = 2;

console.log(window.a);

2

Snippet #4
function foo() {
  var a = 3;
}

console.log(foo.a);

undefined

Snippet #5
function foo() {
  // noop
}

foo.a = 4;

console.log(foo.a);

4

Snippet #6
var a = 5;

function foo() {
  a = 10;

  console.log(a);
  console.log(window.a);
}

foo();

10

10

Section: Cheating Lexical


6. eval. Write the output for the following snippets:
Snippet #7
"use strict";

function foo(str) {
   eval(str);
   console.log(z);
}

foo("var z = 4;");

ReferenceError: z is not defined

Snippet #8
function foo(str) {
   eval(str);
   console.log(x);
}

foo("var x = 5;");

5

Snippet #9
function foo(str) {
   eval(str);
}

foo("y = 7;");

console.log(y);

7

Snippet #10
function foo(str) {
   eval(str);
}

foo("var y = 7;");

console.log(y);

ReferenceError: y is not defined

7. with. Write the output for the following snippets:
Snippet #11
function foo(o) {
   with (o) {
     a = 5;
   }
}

var obj = { a: 2 };

foo(obj);

console.log(obj.a);

5

Snippet #12
function foo(o) {
   with (o) {
     b = 10;
   }
}

var obj = { a: 2 };

foo(obj);

console.log(obj.b);

undefined

Snippet #13
function foo(o) {
   with (o) {
     b = 5;
   }
}

var obj = { a: 2 };

foo(obj);

console.log(b);

5

Snippet #14
function foo(o) {
   with (o) {
     b = 5;
   }
}

var obj = { a: 2 };

foo(obj);

console.log(a);

ReferenceError: a is not defined

Snippet #15
function foo(o) {
   with (o) {
     var b = 3;
   }
   console.log(b);
}

var obj = { b: 2 };

foo(obj);

undefined

Snippet #16
function foo(o) {
   with (o) {
     var b;
   }
   b = 3;
   console.log(b);
}

var obj = { b: 2 };

foo(obj);

3

Snippet #17
function foo(o) {
   with (o) {
     var b = 3;
   }
}

var obj = { b: 2 };

foo(obj);

console.log(b);

ReferenceError: b is not defined

Snippet #18
function foo(o) {
   with (o) {
     b = 3;
   }
}

var obj = { b: 2 };

foo(obj);

console.log(b);

ReferenceError: b is not defined

Snippet #19
"use strict";

function foo(o) {
   with (o) {
     b = 3;
   }
}

var obj = { a: 2 };

foo(obj);

console.log(b);

SyntaxError: Strict mode code may not include a with statement

8. Why should you avoid using both eval and with in your code?

code will run slower no matter how smart the Engine is, because these mechanisms defeat the Engine's ability to perform compile-time optimizations.

9. What's the difference between Lexical Scope and Scope?

none. both are the same, actually is more proper to call it Lexical Scope