I'm reading this post from stackoverflow wiki. And I want to fully understand the following code snippet. Hopefully someone can help with the following questions.
var plane = function(defaultAirport) {
var lastAirportLeft = defaultAirport;
var car = {
driver: {
startAccessPlaneInfo: function() {
setInterval(function() {
console.log("Last airport was " + lastAirportLeft);
}, 20000);
}
}
};
car.driver.startAccessPlaneInfo();
return {
leaveTheAirport: function(airPortName) {
lastAirportLeft = airPortName;
}
}
}("Boryspil International Airport");
plane.leaveTheAirport("John F. Kennedy");
When I open my console, i just check the typeof(plane) and it's an object. But if I call plane("default airport") - it will throw error.I know "Boryspil International Airport" is already passed into plane, but how to cover the existing value with another one? Also, plane is a function object, why I cannot call like plane("XXX")?
after call plane.leaveTheAirport("John F. Kennedy"), the console will print out John F. kennedy instead of Boryspil International Airport. Can anyone explain why the old value from outer scope is replaced with the old one?