Reputation: 75
I have been making a videogame with javascript. However, there is just one thing that I don't understand.
if (!user.hasOwnProperty('firstName')) {
$('#inputSubmit').click(function () {
user.firstName = getInput();
addText_1("Good, now type your character's last name");
});
};
this statement will keep executing. Basically the condition is that the user does't have a first name property and the function will add the first name on the click of submit. However, you can keep pressing the submit button and it will keep adding the text.
$('#inputSubmit').click(function() {
if(!user.hasOwnProperty('firstName')) {
user.firstName = getInput();
addText_1('hello');
};
});
However, this works. It only does it once. Could someone explain the principle that I am not understanding?
Thank you very much!
Upvotes: 0
Views: 44
Reputation: 133
in first code "hasOwnProperty" checked in outer scope of function() but in second code the condition checked in scope of function
Upvotes: 0
Reputation: 11340
You attach a click
event to #inputSubmit
. This callback ignores the surrounding if statement. In your second sample code, if is inside the callback function.
Upvotes: 2