I wrote a small script after learning a bit about the object constructor and I wanted to take it a step further with this code, but the if else isn't behaving as expected... All I want to know is why? I understand this kind of script isn't a normal thing in Javascript... but I believe the === is evaluating to true, thus printing dave in the alert box, no matter what I enter, and not following through the else if conditions.
var dave, jason, nick, get;
get = prompt('Input your Name', 'Name Here')
var Person = function (name, birthYear, job) {
this.name = name;
this.birthYear = birthYear;
this.job = job;
};
dave = new Person('Dave', 1976, 'Showman');
jason = new Person('Jason', 1987, 'Novice');
nick = new Person('Nick', 1993, 'Cable-tech');
function printObj(object) {
var output = '';
for (var property in object) {
output += property + ': ' + object[property] + '\n';
}
alert(output);
}
if (get.toLowerCase === 'dave'.toLowerCase) {
printObj(dave);
} else if (get.toLowerCase === 'jason'.toLowerCase) {
printObj(jason);
} else if (get.toLowerCase === 'nick'.toLowerCase) {
printObj(nick);
} else {
alert('Not a defined object')
}
Something else obvious I might be doing wrong is the comparison... but that's how it was explained to me in ##javascript on freenode, because at first that was my issue, which it still kind of is I suppose. I think I'm simply doing something obviously messy, appreciate any insight I receive, the MDN and w3 only explain so much.
Aucun commentaire:
Enregistrer un commentaire