您现在的位置是:首页 > 电脑技术查询 > web开发

作用链有关问题

编辑:chaxungu时间:2022-10-02 23:16:04分类:web开发

作用链问题
code如下
JScript code var color='blue'; function getcolor(){ var string=color; var color='red'; alert(string);//结果是undefined! } getcolor();
根据作用链规则,应该是输出‘blue’啊,请问为什么会输出undefined?谢谢!

------解决方案--------------------
js是先定义后执行的,你的代码相当于:JScript code var color='blue'; function getcolor(){ var string, color; string = color; color = 'red'; alert(string);//结果是undefined! } getcolor();