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

javascript - jQuery - global variable - Stack Overflow

programmeradmin3浏览0评论

I am trying to use variable in different function, I want to set global variable. Is there way how to do it?

I want something like this:

$('.bar1').animate({'height':'10' + "%"},1500, function() {  
    var bar1_height = $(".bar1").height() * 0.5;  
});

and then use variable bar1_height elsewhere.

I am trying to use variable in different function, I want to set global variable. Is there way how to do it?

I want something like this:

$('.bar1').animate({'height':'10' + "%"},1500, function() {  
    var bar1_height = $(".bar1").height() * 0.5;  
});

and then use variable bar1_height elsewhere.

Share Improve this question edited Jun 27, 2012 at 18:35 Derek 朕會功夫 94.5k45 gold badges198 silver badges253 bronze badges asked Jun 27, 2012 at 18:34 Mario LIPCIKMario LIPCIK 4772 gold badges9 silver badges24 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Declare bar_height outside of your function.

var bar1_height;
$('.bar1').animate({'height':'10' + "%"},1500, function() {
    bar1_height = $(".bar1").height() * 0.5;
});

This will allow you to access it globally (i.e. both inside and outside of your function).

From MDN:

When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.

$('.bar1').animate({'height':'10' + "%"},1500, function() {  
    window.bar1_height = $(".bar1").height() * 0.5;  
});

Done.

Or a more ideal way to do

var bar1_height;
$('.bar1').animate({'height':'10' + "%"},1500, function() {  
    bar1_height = $(".bar1").height() * 0.5;  
});

One of the worst aspects of javascript is implied global scope. You could make your variable global just by dropping the var keyword:

$('.bar1').animate({'height':'10' + "%"},1500, function() {  
   bar1_height = $(".bar1").height() * 0.5;  
});

But this is considered very bad practice. For example:

var getAddress = function(street, city, country) {
    location = street + ', ' + city + ', ' + country;
    return location;
}
getAddress('Holborn', 'London', 'England');

Can you spot the horrendous bug? jsFiddle.

You should really declare your variable in the narrowest scope possible, otherwise you'll end up with a confusing mess of global variables. If you need a variable both inside and outside a function, you should simply declare it in the outer scope (as the other answers have said):

(function () {
    var bar1_height;

    $('.bar1').animate({'height':'10' + "%"},1500, function() {
        // Use variable from outer scope  
        bar1_height = $(".bar1").height() * 0.5; 
    }); 

    // Variable still has its value here
    alert(bar1_height);
})();

(The mysterious outer function here is to prevent the variable from being truly global.)

I found this blog post very useful in understanding best practices regarding variable scope.

发布评论

评论列表(0)

  1. 暂无评论