javascript | 您所在的位置:网站首页 › js定义一个函数 › javascript |
JS的函数定义方式就两种——函数定义(function declaration, abbreviation as FD) 和 函数表达式(function expression, abbreviation as FE)而函数表达式又分为命名与匿名两种。至于构造函数则是函数调用时操作方式与定义无关。FD与FE的具体区别在于:FD是在构建函数的Execution Context时会被计算并作为Activation Object的一个属性被引用,因此就出现declaration hoisting的现象。而FE则是在函数的Runtime中才被计算,而且不会作为Activation Object的一个属性被引用。因此下面的代码在IE9+下无法引用之前的FE (function hello(){console.log('hey, man!')}) try{ hello() } catch(e){ console.log(e) // hello is not defined. }注意:上述代码在IE8下hello可用的。另外FD在recommendation中规定只能出现在全局块和函数块,但Chrome和FF都对此规范进行扩展,因此 if (true){ function test(){} } else{ function test(){} }是合法的。 另外new关键字实际就是 function test(){} var t = new test() var po = Object(), t t = test.call(po) if (!t) t = po |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |