JavaScript Lexical Scope
2015-07-11 00:50
537 查看
I understand them through examples :)
First, Lexical Scope (also called Static Scope), in C-like syntax:
Every inner level can access its outer levels.
There is another way, called Dynamic Scope used by first implementation of Lisp, again in C-like Syntax:
Here
either access
or any
any function that call
in it.
will print 5
will print 10
The first one is called static because it can be deduced at compile-time, the second is called dynamic because the outer scope is dynamic and depends on the chain call of the functions.
I find static scoping easier for the eye. Most languages went this way eventually even Lisp (can do both, right?). Dynamic scoping is like passing references of all variables to the called function.
An example of why the compiler can not deduce the outer dynamic scope of a function, consider our last example, if we write something like this:
The call chain depends on a run time condition. If it is true, then the call chain looks like:
If the condition is false:
The outer scope of fun in both cases is the caller plus the caller of the caller and so on.
Just to mention that C language does not allow nested functions nor dynamic scoping.
First, Lexical Scope (also called Static Scope), in C-like syntax:
void fun() { int x = 5; void fun2() { printf("%d", x); } }
Every inner level can access its outer levels.
There is another way, called Dynamic Scope used by first implementation of Lisp, again in C-like Syntax:
void fun() { printf("%d", x); } void dummy1() { int x = 5; fun(); } void dummy2() { int x = 10; fun(); }
Here
funcan
either access
xin
dummy1or
dummy2,
or any
xin
any function that call
funwith
xdeclared
in it.
dummy1();
will print 5
dummy2();
will print 10
The first one is called static because it can be deduced at compile-time, the second is called dynamic because the outer scope is dynamic and depends on the chain call of the functions.
I find static scoping easier for the eye. Most languages went this way eventually even Lisp (can do both, right?). Dynamic scoping is like passing references of all variables to the called function.
An example of why the compiler can not deduce the outer dynamic scope of a function, consider our last example, if we write something like this:
if(/* some condition */)
dummy1();else
dummy2();
The call chain depends on a run time condition. If it is true, then the call chain looks like:
dummy1 --> fun()
If the condition is false:
dummy2 --> fun()
The outer scope of fun in both cases is the caller plus the caller of the caller and so on.
Just to mention that C language does not allow nested functions nor dynamic scoping.
相关文章推荐
- JS返回上一步
- javascript中JSON对象与JSON字符串相互转换实例
- JavaScript使用RegExp进行正则匹配的方法
- JSON学习总结
- JSON学习总结
- javascript 函数的定义与参数的有无
- 【 D3.js 入门系列 — 2 】 绑定数据和选择元素
- JSONP VS CORS
- JSP深入
- JS DOM编程艺术——最佳实践—— JS学习笔记2015-7-10(第81天)
- JS-商品图片点击轮换
- JS-商品图片放大器
- javascript 事件处理
- Javascript我学之二函数定义
- Js中的window.parent ,window.top,window.self详解
- js 选项卡
- Jsp 自定义tag标签
- [JSOI2007]祖码Zuma/[bzoj]1032
- AFNetworking 使用总结 (用法+JSON解析
- JS中的"=="转换规则