I am having tough time understanding the scope of this
in Javascript.
I am following some tutorial on web but my instructor did something which he advised not to do when explaining b
for example at the start of the instruction he said
var person = "roht"
function whatIsThis() {
return this
console.log(this)
}
function variable (){
this.person = "max"
}
variable()
console.log(person)
whatIsThis()
When I run this.person it will attach value to the global object which is a bad practise (since this have global scope)
but later he himself this thing endless number of time when explaining creating a new object using fucntion constructor
function Person(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
var elie = new Person("james", "roger");
I am sure that I am missing something, can someone help me figure this out?