Revised Question: I came across this code example online, and when I executed it, nothing was logged to the console. I expected it to log "User Name," but it didn't. I'm wondering why this is happening. Why isn't "this" pointing to the newly created object? How does JavaScript determine the reference of "this"? What will happen if I change the normal function to an arrow function, and what if I include a complete condition inside the function?
function createUser() {
// create an object and return from function
return {
name: "User Name",
userRef: this,
};
}
// newly create object assigned to user variable
var user = createUser();
console.info(user.userRef.name);