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

javascript - What is the scope of a 'var' declared inside a function? - Stack Overflow

programmeradmin3浏览0评论

Could someone clarify for me please... I've read that creating a variable using reserved word 'var' makes that variable public but how can that be if the variable was created inside a function:

$('#timeIn').timepicker({ 'scrollDefaultNow': true });
    $('#timeIn').on('change', function() {
        var numIn = $('#timeIn').timepicker(('getSecondsFromMidnight'));
        var inHours = {
            hours: (numIn/86400)*24,
            getter: function() {return this.hours;}
        };
        timeIn = $('#timeIn').val();
        inArray.push(timeIn);
        events.push(timeIn);
});

In this example the variables numIn & inHours are only known within that onChange method, correct? If that is the case what would the global declaration look like? The 'timeIn' is globally scoped but without manipulation I only get a string representation back. What are my options for getting a putable time back as return.

Could someone clarify for me please... I've read that creating a variable using reserved word 'var' makes that variable public but how can that be if the variable was created inside a function:

$('#timeIn').timepicker({ 'scrollDefaultNow': true });
    $('#timeIn').on('change', function() {
        var numIn = $('#timeIn').timepicker(('getSecondsFromMidnight'));
        var inHours = {
            hours: (numIn/86400)*24,
            getter: function() {return this.hours;}
        };
        timeIn = $('#timeIn').val();
        inArray.push(timeIn);
        events.push(timeIn);
});

In this example the variables numIn & inHours are only known within that onChange method, correct? If that is the case what would the global declaration look like? The 'timeIn' is globally scoped but without manipulation I only get a string representation back. What are my options for getting a putable time back as return.

Share Improve this question edited Sep 6, 2012 at 17:21 jbabey 46.7k12 gold badges72 silver badges94 bronze badges asked Sep 6, 2012 at 17:12 MCGRAWMCGRAW 81715 silver badges37 bronze badges 2
  • 1 See JavaScript Variable Scope – user166390 Commented Sep 6, 2012 at 17:33
  • @pst you are correct sir but part of I wanted most was a confirmed rebuttal of the reserve word 'var', when and where to use it and what can see it-but there is a ton of information on scope at your link – MCGRAW Commented Sep 6, 2012 at 17:38
Add a ment  | 

3 Answers 3

Reset to default 10

Using the word var within the function binds it to that function's scope.

Not using the word var makes it public in all functions and all scopes

JavaScript uses function scope - every variable can be seen only from within the same function or a scope higher than it.

An implicit global variable is what happens when you use a variable without first declaring it. In piled languages this would result in a pilation error, but javascript silently declares the variable as a property of the global object (in a browser this is the window object)

$('#timeIn').on('change', function() {
    var numIn; // only available from inside this anonymous handler function
    ... snip ...
    timeIn = $('#timeIn').val(); // this is an implicit global since it has not been declared anywhere
    // an explicit global, for example's sake
    window.someVar = 'foo';
});

With javascript v1.7 you can also establish block scopes via the let keyword:

let(a = 5, b = 1) {
    // a and b are scoped to this block
    console.log(a+b); // 6
}
console.log(a+b); // error

There are only 3 scopes in JavaScript.

x = 1; <- x is in global scope

var, when used outside of a function, will also create a global:

<script type="text/javascript">var x = 1;</script> <- x is in global scope

function () { var x = 1; } <- x is in function scope

for (let i = 0; i < 5; i += 1) {} <- x is in block scope

发布评论

评论列表(0)

  1. 暂无评论