I have two functions that I use as classes: Person and Eye.
Person creates an Eye object in itself. Later on I try to access the eye object created, with an event handler for whenever the user clicks on the web page.
function Eye(){
this.color="Green";
}
function Person() {
this.name="John";
this.eye = new Eye();
//eye=this.eye; // uncomment and everything works!
document.addEventListener("click", function(){
console.log(name); // This works
console.log(eye); // This doesn't work
})
}
var person= new Person();
Why doesn't this work? Making a second variable eye seems to solve the issue but I have no clue why..
eyeseems to solve the issue but I have no clue why.": Because with that you are creating a global variable.thisis an object which at that moment only exists inside the function andeyeis a property of that object.thisis a special variable in JavaScript, have a look at the MDN documentation: developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/….