0

How should I declare an object variable in Javascript? By mistake, I thought I was in PHP and it worked.

$(res.data).each(function(index, value){
                    $src = value.images.fixed_height;
                    $('<a href="'+value.url+'" target="_blank"><img src="' + $src.url + '" width="' + $src.width +'" height="' + $src.height + '"></a>').load(function(){
                        $(this).appendTo("#results").fadeIn();
                    })
                });

Then, when I tried to think in terms of Javascript it didn't work,

$(res.data).each(function(index, value){
                    var src = value.images.fixed_height;
                    $('<a href="'+value.url+'" target="_blank"><img src="' + src.url + '" width="' + src.width +'" height="' + src.height + '"></a>').load(function(){
                        $(this).appendTo("#results").fadeIn();
                    })
                });
whitenoisedb
  • 181
  • 9
  • 2
    I don't see a reason why the second one shouldn't work so your observation might be incorrect. – Felix Kling Mar 15 '14 at 18:02
  • `var someObject = {};` this way you can declare the object in javascript. – Suman Bogati Mar 15 '14 at 18:02
  • 1
    Your first method is creating a global variable. Your second method is creating a fresh local variable upon each invocation of the `.each()` callback. Unless you need this value to persist beyond the duration of the loop, the local variable is much better, particularly for such a generic name as `src`. – jfriend00 Mar 15 '14 at 18:05
  • 1
    I'm with @FelixKling, I can't see why the second example doesn't work. – Andy Mar 15 '14 at 18:11
  • possible duplicate of [What is the function of the var keyword in ECMAScript 262 3rd Edition/Javascript 1.5?](http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-in-ecmascript-262-3rd-edition-javascript) – Bergi Mar 15 '14 at 18:17
  • FYI the $ is usually prepended to variable names to denote a jQuery selection. For example: var $nodes = $('.nodes'). It's just a neat way of distinguishing them from other variables. – Andy Mar 15 '14 at 18:35
  • `$` has absolutely no significance in variable names. It's just valid to use as first character in variables names, just like `_`. So whether you use `src` or `_src` or `$src` doesn't make a difference. What matters is if you use `var` or not. – Felix Kling Mar 15 '14 at 19:01

1 Answers1

0

The only difference between the two is that $src in the first snippet is global (or at least outside the local scope of the anonymous function) and src in the second example is local to that function.

Since the first thing you do is setting the variable, that shouldn't matter for this piece of code, so the second snippet should work exactly as the first one.

The only explanation for different behaviour I can think of, is when another piece of code actually uses this global src, and therefor is affected by your second snippet. But this doesn't sound very likely to me, so my guess would be that your test was wrong and either both snippets work, or neither does.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210