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:
- get score html, check it's > 0 &&
- check multipler = office_loc1 variable &&
- 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
Post a Comment