JS nested function call context problem solved

  • 2020-03-30 02:30:26
  • OfStack

 
<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Document</title> 
</head> 
<script> 
var stu ={ 
m: function(){ 
var self = this; 
console.log(this === stu); // ==> true; 
function f(){ 
//When a nested function is called, this does not point to the context in which the outer function is called
console.log(this === stu); // ==> false; 
 If you want to access an external function this I need to take the external function this Save it in a variable.  
console.log(self === stu); // ==> true; 
} 
f(); 

} 

} 
</script> 
<body> 

</body> 
</html> 

Related articles: