In short, no, you cannot access this variable without creating it, but you can get around it:
Global variable
Assuming that the “legs” may vary as the application launches, you may need to create a “global” variable by assigning a “window” to the legs, an object that will exist throughout the life of the page:
window.legs = 4;
Then, throughout the entire application, you can change this until you are ready to use it in Cats ():
window.legs = user_input;
Global object
You can even assign an object to a window if you want Cats to have other mutable attributes:
window.Cat = {}; window.Cat.legs = 4; window.Cat.has_tail = true; window.Cat.breeds = ["siamese","other cat breed"];
How to use globals
Then, when the Cat object is created later, you can pull data from the window to create a Cat instance:
function Cat(){ this.legs = window.legs;
Travis heeter
source share