Hi guys
Thank you for reading
How can I access this property when there is no name property? case 2
debugger
var Person = function (name) {
this.name = name || 'danbi';
}
// case 1
//
// var cody = new Person('eugene choi');
// console.log(this.name);
// case 2
// window.name ??
var cody = Person("eugene choi");
console.log(window.name);
Case 1: You called new Person -> this is related to Person (an extra-this is created)
You can access name that way:
console.log(cody.name); // eugene choi
Case 2: You called Person WITHOUT new. That means there is no extra “this” (new is responsible for creating a this) and so this is taken from “window” (global scope)
So window.name is correct. (Maybe it’s not intended to use it this way)
console.log(window.name); // eugene choi
A technical better explanation of these things is e.g. here:
1 Like
Thank you for your kindness.
‘this’ concept is still too easy and difficult.
As you mentioned, the problem was solved.
Thanks once again.