- 浏览: 1437901 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
luhouxiang:
写的很不错,学习了
Extjs 模块化动态加载js实践 -
kingkongtown:
如果想改成淘宝后台那样,可以在编辑器批量上传图片呢?
kissy editor 阶段体会 -
317966578:
兄弟我最近也在整jquery和caja 开放一些接口。在git ...
caja 原理 : 前端 -
liuweihug:
Javascript引擎单线程机制及setTimeout执行原 ...
setTimeout ,xhr,event 线程问题 -
辽主临轩:
怎么能让浏览器不进入 文档模式的quirks模式,进入标准的
浏览器模式与文本模式
条件函数定义与异常作用域
好像以前没记录过,罕有情景 firefox 被ie 系列(<=8)以及chrome v8,safari,opera 孤立了:
经典如:
if(true){ function god(){ } }else{ function god(){ alert("what the hell!"); } } alert(god);
不常见如:
alert(typeof z); try{ var x=1; }catch(e){ function z(){} } alert(typeof z);
总结:
函数声明在非firefox下是神,无视障碍会穿越(最后声明的就是事实)!哪个是标准?
updated 2010-10 :
标准与实现
ie,chrome 中函数都会根据 ecma 规范被提前申明,例如:
if(false) { function f(){..} }
等价于
function f(){..} if(false){}
但是 ie 对于具名函数表达式处理有差异。ie 中
if(false){ var f=function f(){ .... }; }
相当于:
function f(){ ... } if(false) { var f=function(){...} }
chrome 最符合标准,具名表达式中的名字只能用于该函数内部递归调用,而不能在当前作用域起作用,则相当于:
if(false){ var f=function(){...}; }
firefox 由于自 javascript1.5 引入了 Conditional function declarations ,则函数申明也可以条件化( firefox 把这种不叫做声明,而叫做表达式 )
if(false){ function f(){..} }
相当于:
if(false){ //不一定是window,只是在当前作用域动态定义 window.f=function(){...} }
详细可见这里的讨论 以及针对jscript的实验 。(相等比较部分看这里 。)
updated 2011-03-04 :
引擎实现
根据 Narcissus 的实现(基于 firefox ),函数定义可分为三种方式:
DECALRED_FORM :
function t(){}
STATEMENT_FORM :
{ function t(){ } }
EXPRESSED_FORM :
var x = function t(){}
而对于 DECALRED_FORM 各个浏览器自然没有疑问。这次主要看看 firefox 的整体函数实现:
switch (n.type) { case FUNCTION: if (n.functionForm != DECLARED_FORM) { /* function(){} 或 { function t(){ } } */ if (!n.name || n.functionForm == STATEMENT_FORM) { v = new FunctionObject(n, x.scope); //如果是有名字,则在当前作用域中加入名字 if (n.functionForm == STATEMENT_FORM) x.scope.object[n.name] = v; } /** var x=function t(){}; **/ else { //新生成一个作用域,里面只有名字 t ,当前作用域引不到 x.scope = {object: {}, parent: x.scope}; try { //只能在函数中引到本函数的名字 t v = new FunctionObject(n, x.scope); // t 加入到新的作用域 x.scope.object[n.name] = v; } finally { //恢复 x.scope = x.scope.parent; } } } break;
update 2011-08-16
function and function scope
细读 function and function scope @ mdc :
function x(){}
相当于函数名为 x ,并且在当前 scope 加了一个变量 x 指向函数 x.
看起来和函数表达式情况类似:
var x=function x(){}
明显了除了解析阶段不同,还有一个微小的差别:
函数声明不会绑定函数名到函数体作用域,而函数表达式则会将将函数名绑定到函数体作用域,而函数名是不可变的,可变的只是那个在当前作用域指向函数的变量:
函数声明没有绑定函数名到函数体:
(function () { function x(zz) { if (zz == 2) { alert("o"); return; } x(2); } var z = x; x = 1; z(); })();
函数表达式绑定了函数名到函数体:
(function () { var x = function x(zz) { if (zz == 2) { alert("o"); return; } x(2); } var z = x; x = 1; z(); })();
catch 内变量声明问题:
try{ var x=1; throw x; }catch(e){ var x=2; } alert(typeof e); alert(x);
除了 ie<9 ( ie9 e 也会 scope leak 到上一层作用域 )外其中 e 是没问题的,但是x就很奇怪了,根据ecma规范,catch 会新建一个词法环境,e以及在里面申明的变量不会和外边的混在一起的?catch退出后新的词法环境就消失了,怎么x会被修改呢?
The production Catch : catch ( Identifier ) Block is evaluated as follows:
1. Let C be the parameter that has been passed to this production.
2. Let oldEnv be the running execution context’s LexicalEnvironment.
3. Let catchEnv be the result of calling NewDeclarativeEnvironment passing oldEnv as the argument .
4. Call the CreateMutableBinding concrete method of catchEnv passing the Identifier String value as the
argument.
5. Call the SetMutableBinding concrete method of catchEnv passing the Identifier, C, and false as arguments. Note that the last argument is immaterial in this situation.
6. Set the running execution context’s LexicalEnvironment to catchEnv.
7. Let B be the result of evaluating Block.
8. Set the running execution context’s LexicalEnvironment to oldEnv.
9. Return B.
updated 2011-08-16:
异常 e 应该是插入到新建的申明作用域头,但这个声明作用域比较特殊,则其内声明的变量其实会声明在父作用域,这个新的作用域在查找时起作用,只有在这个作用域能够找到 e
Refer:
http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting
http://www.dustindiaz.com/javascript-function-declaration-ambiguity/
发表评论
-
continuation, cps
2013-09-12 16:49 2732起 随着 nodejs 的兴起,异步编程成为一种潮流 ... -
一种基于匹配回朔的 css3 选择器引擎实现
2013-05-07 20:40 3356一种基于匹配回朔的 css3 选择器引擎实现 介绍 C ... -
cubic-bezier 模拟实现
2013-01-05 16:34 14027cubic-bezier 曲线是 css3 动画的一个重要基石 ... -
构建前端 DSL
2012-10-11 22:10 5292目前在传统的软件开 ... -
Get cursor position and coordinates from textarea
2012-04-10 20:50 4943最近需要从 textarea 中获 ... -
兼容 ie 的 transform
2012-02-23 14:00 6349css 2d transform 是 css3 引入的一个新的 ... -
promise api 与应用场景
2012-02-07 17:34 7299promise 是 commonjs 社区中提出的异步规范,其 ... -
closure compiler 代码优化实例
2012-01-08 03:23 2780closure compiler 可以进行不少有意思的优化 ... -
write html parser
2011-12-01 02:48 2863首先需要声明 html 不能用正则表达式来直接匹配进行内容抽取 ... -
获取剪贴板数据
2011-11-07 23:31 6396兼容性: 获取剪贴板数据这块各个浏览器间存在很大的 ... -
url 映射问题
2011-11-07 21:52 3173背景 url mapping 我最早知道是作为 j ... -
tip:如何原生播放声音
2011-10-19 12:45 2910如果不想考虑浏览器间 ... -
转载:瀑布流布局浅析
2011-09-29 19:02 2798简介 如果你经 ... -
cross domain request
2011-09-29 18:39 2803场景 跨域请求是随着 ... -
基于多继承的树设计
2011-09-18 03:42 2219分类 树是一种常见 ... -
caja 原理 : 前端
2011-09-01 16:48 6963作为前端开放的基础安全保证,caja 是目前比较合 ... -
tokenization of html
2011-08-29 22:38 2693html 符号解析问题 场景: 在页面上输出包 ... -
ie 下 cloneNode 导致的属性克隆
2011-08-24 16:10 2426这个还是很值得记下,一直存在的很大隐患终于解决,由于在 ie& ... -
循环引用下的深度克隆
2011-08-04 20:39 2241深度复制和浅度复制 是当初初学 c 遇到的第一批问题,似乎使 ... -
模块的静态与动态循环依赖
2011-07-25 03:43 3210场景: 循环依赖 我是不支持的,但现实中似乎又确实需 ...
相关推荐
fs/yaffs2/yaffs_vfs.c:2383:2: error: implicit declaration of function 'get_sb_bdev' fs/yaffs2/yaffs_vfs.c: At top level: fs/yaffs2/yaffs_vfs.c:2390:2: error: unknown field 'get_sb' specified in ...
1."c" not an argument in function sum 该标识符不是函数的参数 2.array bounds missing ] in function main 缺少数组界限符 "]" ...10.declaration syntax error in function main 宣告语法错误 ……
西门子_CE DECLARATION of CONFORMITYpdf,
_ ByVal wFlags As Integer) 'Play Sound Function declaration Declare Function sndPlaySound Lib _ "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, _ ByVal uFlags As Long) As Long ' ...
17-declaration-merging(声明合并17).pdf
RzBorder.pas Number of elements differs from declaration TRzLEDCharacters 在装完Raize控件之后编译老是出如下错误: [Pascal Error] RzBorder.pas(1429): E2072 Number of elements (125) differs from ...
爱思唯尔的利益声明模板 论文已正式出版~ 相关博客:https://blog.csdn.net/qq_39763246/article/details/126797385
requirement analysis •conceptual database design •choice of the DBMS •data model mapping •physical design •implementation
android 7.1的shortcuts功能demo
施耐德电气 Twido可编程控制器产品认证——CE Manufacturer’s Declaration TWDpdf,施耐德电气 Twido可编程控制器产品认证——CE Manufacturer’s Declaration TWD
SqList_Declaration.h
FPGA常见问题解答FPGA常见问题解答
智能科学专业本科生学习智能技术课程的参考书,计算机视觉方向
an algorithm for equivelence declaration
Hadoop in Practice collects 85 Hadoop examples and presents them in a problem/solution format. Each technique addresses a specific task you'll face, like querying big data using Pig or writing a log ...
什么是 Function Declaration(函数声明)? Function Declaration 可以定义命名的函数变量,而无需给变量赋值。Function Declaration 是一种独立的结构,不能嵌套在非功能模块中。可以将它类比为 Variable ...
默认编译后,再回头编译vlc开源库,发现:lua/demux.c:55:13: 错误:implicit declaration of function ‘luaL_checkint’; did you mean ‘luaL_checkany’ 经过查找后发现,此gcc中宏定义没有生效,可能加载顺序出...
c#网络编程,编写网络应用程序是程序设计中最有趣的领域之一。眼看着自己编写的程序成功地通过网络实现了通信,这是特别令人振奋的
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_...
TI-EU Declaration of Conformity (DoC)U Declaration of Conformity (DoC