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 computable time back as return.
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 compiled languages this would result in a compilation error, but javascript silently declares the variable as a property of the global object (in a browser this is the
windowobject)With javascript v1.7 you can also establish block scopes via the let keyword: