Why can you print out numbers without defining global variables?

description of the problem: in the following code, var is called globally. The this points to window, and the name variable is not defined under window. Why does the global call c () output 1 instead of undefined

?
var a={
   name:2,
   b:function(){
    this.namePP
    console.log(this.name) 
   } 
}
 a.b()//3
 var c=a.b
 c()//1
Mar.24,2021

The

window object itself has the name attribute, which represents the name of the window. The default is the empty string ", and the type is the string type. If the name attribute of window is not assigned as a string type, forces to be converted to a string type.
when executing c () , this points to window , that is,

window.namePP
console.log(window.name)

window.name defaults to ". The empty string becomes 1 when performing a self-increment operation, and is forced to be converted to a string because it is assigned to window.name , that is, window.name is " 1 ".

When

var c=a.b, this points to window, so a global name


is defined under window.
this points to window, and there is no name variable defined under window

?
window.name learn about

Menu