I just wish to know if there is a simple way to call a variable from another function within the same Object?
var comedy = {
schadenfreude: function() {
var number = prompt("Provide me a number.");
},
irony: function() {
this.schadenfreude();
for(i = 0; i < this.schadenfreude.number(); i++){
return i;
}
}
};
console.log(comedy.irony());
In this example I wish to grab the var number from the schadenfreude function and use that prompted number in my for loop with the irony function. Thanks for the help.
The “number” variable in your “schadenfreude” function is local to that function. Even if you could get to it (you can’t), it would do you no good.
What you (probably) want is:
Alternatively, you could have “schadenfreude” assign the value to a “number” property on the object, so that you’d refer to
this.numberin the “irony” function. In either case, as I noted in the comment in the code, the fact that yourforloop has thatreturnstatement means that the “irony” function will always return zero if the user gives you any non-negative number, andundefinedotherwise.