Skip to content

Latest commit

 

History

History
356 lines (232 loc) · 3.76 KB

File metadata and controls

356 lines (232 loc) · 3.76 KB

Quiz - YDKJS: Scope & Closures 2/5

Chapter 2. Lexical Scope

Self-Evaluation

Section: Lex-time


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

your answer here

2. Which is the Scope model that JavaScript employs?

your answer here

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);

your answer here

4. What is shadowing?

your answer here

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));

your answer here

Snippet #3
var a = 2;

console.log(window.a);

your answer here

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

console.log(foo.a);

your answer here

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

foo.a = 4;

console.log(foo.a);

your answer here

Snippet #6
var a = 5;

function foo() {
  a = 10;

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

foo();

your answer here

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;");

your answer here

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

foo("var x = 5;");

your answer here

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

foo("y = 7;");

console.log(y);

your answer here

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

foo("var y = 7;");

console.log(y);

your answer here

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);

your answer here

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

var obj = { a: 2 };

foo(obj);

console.log(obj.b);

your answer here

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

var obj = { a: 2 };

foo(obj);

console.log(b);

your answer here

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

var obj = { a: 2 };

foo(obj);

console.log(a);

your answer here

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

var obj = { b: 2 };

foo(obj);

your answer here

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

var obj = { b: 2 };

foo(obj);

your answer here

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

var obj = { b: 2 };

foo(obj);

console.log(b);

your answer here

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

var obj = { b: 2 };

foo(obj);

console.log(b);

your answer here

Snippet #19
"use strict";

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

var obj = { a: 2 };

foo(obj);

console.log(b);

your answer here

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

your answer here

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

your answer here