I am really confused about why this function is still working despite not declaring the variables to pass in the function.
So I started it like this:
let player1;
let player2;
let player;
let showPlayerInfo;
player1 = {
name: 'Luis',
health: 70,
place: 'The Start',
feetHair: '',
items: 'a mobile, a watch, a 20-pound bill'
};
player2 = {
name: 'Anna',
health: 70,
place: 'The Great Door',
feetHair: '',
items: 'a broom, a TV set, a 10-pound bill'
};
// HERE I AM DECLARING THE VARIABLE "PLAYER" INSIDE THE FUNCTION
showPlayerInfo = function(player){
console.log(`Player name is ${player.name} with health at ${player.health}.`);
console.log(`It all starts at ${player.place}.`);
console.log(`${player.name} has ${player.items} on them.`);
}
// HERE I AM CALLING THE FUNCTION DECLARING THE VARIABLE "PLAYER" TO BE USED
player = player1;
showPlayerInfo(player);
// HERE I AM CALLING THE FUNCTION DECLARING THE VARIABLE "PLAYER" TO BE USED
player = player2;
showPlayerInfo(player);
//IT WERKS!!!!
Ok, the code above works fine, so I go one step further and undeclare the variable "player" from the function, and the function still works because, I think, I am hard-coding the variable name in the function calls:
// I AM NOT DECLARING THE VARIABLE "PLAYER" INSIDE THE FUNCTION
showPlayerInfo = function(){
console.log(`Player name is ${player.name} with health at ${player.health}.`);
console.log(`It all starts at ${player.place}.`);
console.log(`${player.name} has ${player.items} on them.`);
}
// Still calling function with variable "player"
player = player1;
showPlayerInfo(player);
// Still calling function with variable "player"
player = player2;
showPlayerInfo(player);
//IT WERKS!!!!
And the code above still works. I go one step even further and completely undeclare the variable name 'player' from both function and the function calls and I can't understand how it even knows where to get the variable from? What kind of sorcery is this? The code below still works:
// I AM NOT DECLARING THE VARIABLE "PLAYER" INSIDE THE FUNCTION
showPlayerInfo = function(){
console.log(`Player name is ${player.name} with health at ${player.health}.`);
console.log(`It all starts at ${player.place}.`);
console.log(`${player.name} has ${player.items} on them.`);
}
// Not declaring variable "player" from the call
player = player1;
showPlayerInfo();
// Not declaring variable "player" from the call
player = player2;
showPlayerInfo();
//IT WERKS! BUT HOW???
Lastly, I did an experiment by undeclaring the variable from the function calls but keeping it in the function itself, and the code returns an error (Uncaught TypeError: Cannot read property 'name' of undefined at showPlayerInfo). What??? Why???:
// I AM DECLARING THE VARIABLE "PLAYER" INSIDE THE FUNCTION
showPlayerInfo = function(player){
console.log(`Player name is ${player.name} with health at ${player.health}.`);
console.log(`It all starts at ${player.place}.`);
console.log(`${player.name} has ${player.items} on them.`);
}
// Not declaring variable "player" from the call
player = player1;
showPlayerInfo();
// Not declaring variable "player" from the call
player = player2;
showPlayerInfo();
//IT DOESNT WORK! BUT WHY???