最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - global variable in javascript? - Stack Overflow

programmeradmin0浏览0评论

i have this code:

    $(".link").each(function() {
            group += 1;
            text += 1;
            var links = [];
            links[group] = [];

            links[group][text] = $(this).val();
        }
    });

    var jsonLinks = $.toJSON(links);

after it has looped every .link it will exit the each loop and encode the array 'links' to json. but the array 'links' is a local variable inside the each-loop. how can i make it global outside the loop?

i have this code:

    $(".link").each(function() {
            group += 1;
            text += 1;
            var links = [];
            links[group] = [];

            links[group][text] = $(this).val();
        }
    });

    var jsonLinks = $.toJSON(links);

after it has looped every .link it will exit the each loop and encode the array 'links' to json. but the array 'links' is a local variable inside the each-loop. how can i make it global outside the loop?

Share Improve this question asked Jan 26, 2010 at 8:50 ajsieajsie 79.9k110 gold badges284 silver badges386 bronze badges 2
  • Possible dupe ( stackoverflow./questions/2138287/… )? – Travis Gockel Commented Jan 26, 2010 at 8:53
  • 1 Your question appeared twice. Better to delete this one. – kgiannakakis Commented Jan 26, 2010 at 8:53
Add a ment  | 

6 Answers 6

Reset to default 9

Define links outside the loop:

var links = [];
$(".link").each(function() {
  group += 1;
  text += 1;
  links[group] = [];
  links[group][text] = $(this).val();
});
var jsonLinks = $.toJSON(links);

I should also point out that this doesn't make a lot of sense because you will end up element 7, for example, being an array with a single element (indexed as 7) to the value. Is this really what you want?

What I imagine you want is an array of the values. If so, why not use map()?

var links = $(".link").map(function(i, val) {
  return $(val).val();
});

.

var links = [];

$(".link").each(function() {
        group += 1;
        text += 1;            
        links[group] = [];

        links[group][text] = $(this).val();
    }
});

var jsonLinks = $.toJSON(links);

Just declare it before your block of code:

var links = [];
$(".link").each(function() {
        group += 1;
        text += 1;
        links[group] = [];
        links[group][text] = $(this).val();
    }
});

var jsonLinks = $.toJSON(links);

or simply remove 'var':

$(".link").each(function() {
        group += 1;
        text += 1;
        links = [];
        links[group] = [];
        links[group][text] = $(this).val();
    }
});

var jsonLinks = $.toJSON(links);

Create a closure:

{    
    var links = [];
    $(".link").each(function() {
            group += 1;
            text += 1;

            links[group] = [];

            links[group][text] = $(this).val();
        }
    });

    var jsonLinks = $.toJSON(links);
}

To define a global variable, you can either

a) define the variable outside of a function (as already mentioned in other answers)

or

b) attache the variable to the window object


$(".link").each(function() {
            group += 1;
            text += 1;
            window.links = [];
            links[group] = [];

            links[group][text] = $(this).val();
        }
    });

    var jsonLinks = $.toJSON(links);

or

c) create a variable without the var keyword


$(".link").each(function() {
            group += 1;
            text += 1;
            links = [];
            links[group] = [];

            links[group][text] = $(this).val();
        }
    });

    var jsonLinks = $.toJSON(links);

JavaScript only understand two scopes

  1. Global: Which is any variable outside the function and variables declare without the var

  2. Function : Anything which is inside the function.

So i will suggest you the closure approach as follows

 function getJSONLinks()
 {
      var links = [];
      $(".link").each(function() {
        group += 1;
        text += 1;
        links[group] = [];
        links[group][text] = $(this).val();
       }
     });
     return $.toJSON(links);
 }
发布评论

评论列表(0)

  1. 暂无评论