Adding properties to global object in JS

41 views Asked by At

why adding a property to globalThis inside a function doesn't work?

let foo = function(){
    this.name='Global Elite'
}
foo()
console.log(this.name) // undefined

This gets more confusing because if I use the arrow function instead the function keyword it can add the name property to global object.

let foo = ()=>{
    this.name='Global Elite'
}
foo()
console.log(this.name) // Global Elite

Edit: I ran the above code in node js.

0

There are 0 answers