sniperman
03-10-2015, 12:57 PM
I have started to work with JS after a long hiatus, and wanted to create a simple logic to store values and hold their state on each call back.
I attempted to use Constructors. I also attempted to use an Object/Function but that did returned "object is not defined" often.
The code below works to return a new value but I would like to include the JavaScript within the FORM element into the SCRIPT section and create a function/constructor/prototype and call the function from within the FORM element.
I know there are better methods than mine to do this, and keep the array state so each callback can add to the array value.
var monster = {
arms: 0,
legs: 0,
bodies: 0,
colors: 0,
hair: 0,
/* AddAppendage: function() { // randomly add one of the appendages
var x = "which appendage";
return x;
}, */
};
alert(Object.getOwnPropertyNames(monster)[Math.floor(Math.random()*5)]); // returns one of the appendages
<input name="input" value="enter a number" onfocus="this.value=''" onchange="monster.arms+=parseInt(this.value);alert('this monster has ' + monster.arms + ' arms')" ></input>
I attempted to use Constructors. I also attempted to use an Object/Function but that did returned "object is not defined" often.
The code below works to return a new value but I would like to include the JavaScript within the FORM element into the SCRIPT section and create a function/constructor/prototype and call the function from within the FORM element.
I know there are better methods than mine to do this, and keep the array state so each callback can add to the array value.
var monster = {
arms: 0,
legs: 0,
bodies: 0,
colors: 0,
hair: 0,
/* AddAppendage: function() { // randomly add one of the appendages
var x = "which appendage";
return x;
}, */
};
alert(Object.getOwnPropertyNames(monster)[Math.floor(Math.random()*5)]); // returns one of the appendages
<input name="input" value="enter a number" onfocus="this.value=''" onchange="monster.arms+=parseInt(this.value);alert('this monster has ' + monster.arms + ' arms')" ></input>