javascript - How to update my function expression without state variables? -


i'm creating game in jquery , player created on click of button. function expression use hold it's info:

var getplayerdata = function() {     return {         name: $("#name").val(),//get name input         office_name: $("#office-name").val(), //get name input         score: parseint($('#score').text(), 10), //players startwith 1k hc         office_location: "office_loc1", //set player's office         office_multiplier: office_loc1, // set players office multiplier         notified: false,         projects_completed: 0     }; }; 

then run function within other functions access player's variables, so:

$('#create-player').click(function startproject() {     getplayerdata(); }); 

i need way update both office_location , office_multiplier variables using conditional. office multipliers referencing set of variables:

var office_loc1 = .01,     office_loc2 = .02,     office_loc3 = .03,     office_loc4 = .04,     office_loc5 = .05, 

this conditional trying update of functions, here steps if conditional:

  1. get score html, check it's > 0 &&
  2. check multipler = office_loc1 variable &&
  3. check if player_notified = false

if (parseint($('#score').text(), 10) > 0 && getplayerdata().office_multiplier === office_loc1 && getplayerdata().notified === false){ }

the key parts pay attention in conditional are:

//upgrade players office , notify them       getplayerdata().office_location = "office_loc2";       getplayerdata().office_multiplier = office_loc2;       getplayerdata().notified = true; 

however, not update variable. have ideas on this?

thanks!

you creating new object instance of player each time call getplayerdata

if read-only object, wouldn't problem, once try mutate property of it, loose updated value once call getplayerdata() again.

you should keep playerdata instance variable, can access through code.

var playerdata = getplayerdata(); 

------------- edit -------------

right after defining getplayerdata function, create var playerdata. this:

var getplayerdata = function() {     return {         name: $("#name").val(),//get name input         office_name: $("#office-name").val(), //get name input         score: parseint($('#score').text(), 10), //players startwith 1k hc         office_location: "office_loc1", //set player's office         office_multiplier: office_loc1, // set players office multiplier         notified: false,         projects_completed: 0     }; }; var playerdata = getplayerdata(); 

then, on everyplace call getplayerdata(), use playerdata instead.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -