这是自己的处女篇,就照葫芦画瓢吧!
关于js的词法分析首先要理解变量作用域,变量作用域是在定义时决定而不是在执行时,也就是词法作用域取决于源码,通过静态分析就能确定。
词法分析分析些什么?
1,首先,分析参数
2,然后,分析变量声明
3,再然后,分析函数声明
下面通过例子来说明:
function a(age) {
<span style="white-space:pre"> </span>alert()
<span style="white-space:pre"> </span>var age = 10;
<span style="white-space:pre"> </span>function age(){
<span style="white-space:pre"> </span>alert()
<span style="white-space:pre"> </span>}
};
a(5);
<span style="color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;"><span style="white-space:pre"> </span>具体步骤:</span><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;" /><span style="color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;"> <span style="white-space:pre"> </span>Ⅰ:在函数运行前的一瞬间,生成Active Object(活动对象),下称AO;</span><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;" /><span style="color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;"> <span style="white-space:pre"> </span>Ⅱ:把声明的参数,形成AO的属性,值全是undefined;紧接着传入实参5,即age = 5;</span><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;" /><span style="color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;"> <span style="white-space:pre"> </span>Ⅲ:分析变量声明! 如var age</span><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;" /><span style="color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;"> <span style="white-space:pre"> </span>如果AO上还没有age属性,则添加AO属性,值全是undefined</span><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;" /><span style="color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;"> <span style="white-space:pre"> </span>如果AO上已有age属性,则不做任何影响</span><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;" /><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;" /><span style="color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;"> <span style="white-space:pre"> </span>Ⅳ:分析函数声明, 如function age (){},</span><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;" /><span style="color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;"> <span style="white-space:pre"> </span>则把函数赋给AO.age属性,即age=function(){alert()};</span>