js之局部变量转换全局变量教程
程序员文章站
2023-12-16 14:30:01
js——局部变量转换全局变量
function enjoy1(){
var num1=100;
}...
js——局部变量转换全局变量
function enjoy1(){ var num1=100; } enjoy1(); console.log(num1);//报错:num is not defined
1、
function enjoy2(){ var num2=100; window.num2=num2; } enjoy2(); console.log(num2);//100
2、
function test(){ var age=19;//局部 return function(){ console.log(age); } } var func=test();//获取局部变量,这一步不可少 func();//19