0

I've been exploring scope within JavaScript. Sources point out that scope is function-delimited, not block-delimited as in most languages.

I put some display code in some code I'm writing, because some of my functions-within-functions are not clear (to me), and I wanted to see just how the scope works for these.

The big surprise is that in the $.each function within the $.getJSON function the if(){} clause is, evidently, treated as a function. I would have assumed it to be a block.

function displayInfo(nKey) {
    if(!nKey) var nKey = 0;
    var objFilm = {};
    var imgRef;
    //iterate through all object properties; display their attributes
    // Object.keys() returns an array of all property names
    // for most entries, the object is ...film; check first for array of multiple films
    jqxhr = $.getJSON('dbMovies.json', function(data) {
        var xx = "xx";
        $.each(data.disc, function(i, xdata) {
            if(xdata.key == nKey) {
                objFilm = xdata.film;
                var yy = "yy";
                imgRef = xdata.img;
                return false;
            }
console.log("in jqxhr, xx: " + typeof xx);  //this shows
console.log("in jqxhr, yy: " + typeof yy);  //this does NOT
        }); // $.each
    })
    .done(function() {...}

If if(){} is a function, what is a block?

4
  • 1
    no. it's NOT a function. if it was a function. it could have a return value. var foo = if(...) {... } is a flat-out syntax error. it's a keyword. and note that your if() inside the .each is actually inside a function: function(i,xdata) ... Commented Feb 12, 2016 at 17:31
  • if isn't a function. What makes you think it's a function? Commented Feb 12, 2016 at 17:31
  • You can read about statements in the language specification here : ecma-international.org/ecma-262/5.1/#sec-12 :) Commented Feb 12, 2016 at 17:31
  • Thanks. I see that now. Commented Feb 12, 2016 at 19:04

2 Answers 2

8

You have a return false inside the if, therefore the only time those two log statements will be reached is if the if condition wasn't true and the block controlled by the if didn't run. And if the block didn't run, then yy wasn't assigned a value. It's in scope, but uninitialized.

Sign up to request clarification or add additional context in comments.

2 Comments

Does return false even work there? I know it's useless with forEach, not so sure about $.each...
@Andy Returning false from $.each stops the loop.
2

The variable the function is assigned to is limited only by its scope, which is guaranteed to include the scope where the function is declared in.

if() { } is not a function... it is a block. the reason it is not showing up in your example is because you have return false which breaks before the log happens, and when the logs do happen then the variable is still undefined.

(function () {

  if (true) var x = "if declared variable"; /* block declaration */
  
  document.getElementById('if').innerHTML = x ? x : 'undefined';
  
  (function() {
    var y = "function declared variable";
  })();
  
  if (typeof y != 'undefined')
      document.getElementById('func').innerHTML = y
  else
      document.getElementById('func').innerHTML = 'undefined';
  
})();
if: <span id="if"></span>
<br>
func: <span id="func"></span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.