Get Programming with JavaScript - Listing 4.14

Listing 4.14 - A function to display player information

var player1; var player2; var player; var showPlayerInfo; player1 = { name: "Kandra", place: "The Dungeon of Doom", health: 50 }; player2 = { name: "Dax", place: "The Old Library", health: 40 }; showPlayerInfo = function () { console.log(player.name); console.log("------------------------------"); console.log(player.name + " is in " + player.place); console.log(player.name + " has health " + player.health); console.log("------------------------------"); console.log(""); }; player = player1; showPlayerInfo(); player = player2; showPlayerInfo();

Further Adventures

Listing 4.14 - A function to display player information - Task 1

var player1; var player2; var player; var showPlayerInfo; var showPlayerPlace; // Declare a variable player1 = { name: "Kandra", place: "The Dungeon of Doom", health: 50 }; player2 = { name: "Dax", place: "The Old Library", health: 40 }; // Define a new function // Assign it to a variable showPlayerPlace = function () { console.log(player.name + " is in " + player.place); }; showPlayerInfo = function () { console.log(player.name); console.log("------------------------------"); console.log(player.name + " is in " + player.place); console.log(player.name + " has health " + player.health); console.log("------------------------------"); console.log(""); }; player = player1; showPlayerInfo(); player = player2; showPlayerInfo();

Listing 4.14 - A function to display player information - Task 2

var player1; var player2; var player; var showPlayerInfo; var showPlayerPlace; var showPlayerHealth; // Declare a variable player1 = { name: "Kandra", place: "The Dungeon of Doom", health: 50 }; player2 = { name: "Dax", place: "The Old Library", health: 40 }; showPlayerPlace = function () { console.log(player.name + " is in " + player.place); }; // Define a new function // Assign it to a variable showPlayerHealth = function () { console.log(player.name + " has health " + player.health); }; showPlayerInfo = function () { console.log(player.name); console.log("------------------------------"); console.log(player.name + " is in " + player.place); console.log(player.name + " has health " + player.health); console.log("------------------------------"); console.log(""); }; player = player1; showPlayerInfo(); player = player2; showPlayerInfo();

Listing 4.14 - A function to display player information - Task 3

var player1; var player2; var player; var showPlayerInfo; var showPlayerPlace; var showPlayerHealth; player1 = { name: "Kandra", place: "The Dungeon of Doom", health: 50 }; player2 = { name: "Dax", place: "The Old Library", health: 40 }; showPlayerPlace = function () { console.log(player.name + " is in " + player.place); }; showPlayerHealth = function () { console.log(player.name + " has health " + player.health); }; // Use the two new functions showPlayerInfo = function () { console.log(player.name); console.log("------------------------------"); showPlayerPlace(); showPlayerHealth(); console.log("------------------------------"); console.log(""); }; player = player1; showPlayerInfo(); player = player2; showPlayerInfo();