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
3 Answers
Reset to default 10Using 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