javascript statically scope
在javascript 里面, 函数中使用的未定义的变量,会默认变为全局的变量。 而通过 var 这个关键字定义的变量,就是局部变量。
As far as the output is concerned myVar and addMe both will be global variable in this case , as in javascript if you don't declare a variable with var then it implicitly declares it as global hence when you call runMe() then myVar will have the value 10 and addMe will have 20 .
---------------------------------------------------------------------------------------------------------------------------
Are variables statically or dynamically “scoped” in javascript?
Or more specific to what I need:
If I call a function from within another function, is it going to pull the variable from within the calling function, or from the level above? Ex:
myVar=0;
function runMe(){
myVar = 10;
callMe();
}
function callMe(){
addMe = myVar+10;
}
What does myVar end up being if callMe() is called through runMe()?
-----------------------------------------------------------------------------------------------------
Jeff is right. Note that this is not actually a good test of static scoping (which JS does have). A better one would be:
myVar=0;
function runMe(){
var myVar = 10;
callMe();
}
function callMe(){
addMe = myVar+10;
}
runMe();
alert(addMe);
alert(myVar);
In a statically scoped language (like JS), that alerts 10, and 0. The var myVar (local variable) in runMe shadows the global myVar in that function. However, it has no effect in callMe, so callMe uses the global myVar which is still at 0.
In a dynamically scoped language (unlike JS), callMe would inherit scope from runMe, so addMe would become 20. Note that myVar would still be 0 at the alert, because the alert does not inherit scope from either function.
javascript statically scope的更多相关文章
- javascript作用域(Scope),简述上下文(context)和作用域的定义
网页制作Webjx文章简介:这篇文章将正面解决这个问题:简述上下文(context)和作用域的定义,分析可以让我们掌控上下文的两种方法,最后深入一种高效的方案,它能有效解决我所碰到的90%的问题. 作 ...
- JavaScript作用域[[scope]]
[[scope]] : 隐式的属性 每个JavaScript函数都是一个对象,对象中有些属性可以访问,而有些属性是不可以访问的,这些属性仅供JavaScript引擎存取, [[scope]]就是其中一 ...
- [Javascript] Function scope
We have code like: var numbers = [1,2,3]; for(var i in numbers){ setTimeout(function(){console.log(n ...
- JavaScript闭包的底层运行机制
转自:http://blog.leapoahead.com/2015/09/15/js-closure/ 我研究JavaScript闭包(closure)已经有一段时间了.我之前只是学会了如何使用它们 ...
- JAVASCRIPT的一些知识点梳理
春节闲点,可以安心的梳理一下以前不是很清楚的东东.. 看的是以下几个URL: http://web.jobbole.com/82520/ http://blog.csdn.net/luoweifu/a ...
- JavaScript的语法要点 3 - Calling Context
上一篇讲了JavaScript的Scope Chain - 每一个函数都有一个scope chain与之关联,scope chain上有第一个对象维护着本地变量作为其属性.另外我们在JavaScrip ...
- Scope Directive
---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...
- How do JavaScript closures work?
Like the old Albert Einstein said: If you can't explain it to a six-year-old, you really don't under ...
- JavaScript 作用域链解析
JavaScript 中有 Scope( 作用域 ) , Scope chain( 作用域链 ) , Execute context( 执行上下文 ) , Active Object ( 活动对象 ) ...
随机推荐
- C# 方法 虚方法的调用浅谈 引用kdalan的博文
我们在面试中经常碰到有关多态的问题,之前我也一直被此类问题所困扰,闹不清到底执行哪个方法. 先给出一道简单的面试题,大家猜猜看,输出是? public class A { ...
- COMMENT - 定义或者改变一个对象的评注
SYNOPSIS COMMENT ON { TABLE object_name | COLUMN table_name.column_name | AGGREGATE agg_name (agg_ty ...
- Axis1.4框架 实现webservice服务器和客户端
一:软件环境 win7旗舰版, Eclipse,JDK1.6,tomcat6.0,Axis1.4的包. 至于Axis1.4包网上可以下载,如果是在找不到可以留言给我. 二:摘要 将解压后的 axis- ...
- 08C#事件
C#事件 1.2 事件 事件是C#语言内置的语法,可以定义和处理事件,为使用组件编程提供了良好的基础. 1.16.1 事件驱动 Windows操作系统把用户的动作都看作消息,C# ...
- UML-画类图与交互图的顺序
并行.画完交互图,在画类图.交替进行.
- JVM优化(上)
02.我们为什么要对jvm做优化: 1.标准参数:-help-version 2. -X参数(非标) -Xint-Xcomp -Xint : interpreted-Xcomp: complied ...
- 「 Luogu P2196 」 挖地雷
# 解题思路 跑 $\text{n}$ 遍 $\text{spfa}$ 并记录路径,找到比当前最长路长的就更新答案,并且将路径也更新,注意起点的处理. # 附上代码 #include <iost ...
- 对拍 bat命令快速模板
对拍.bat @echo off :loop maker.exe > in.in wq.exe < in.in > out.out std.exe < in.in >st ...
- mysql事物隔离
1.读未提交(行锁) 会发生脏读,事物未提交被其他事物看到,未提交的数据为脏数据. 2.读已提交(行锁) 会发生不可重复读,事物开始时,只能看到已经提交了的事物修改. 3.重复读(行锁) 该级别保证了 ...
- NodeJs中数据库的使用
另一遍通用的NODEJS数据库方法koa,express,node 通用方法连接MySQL 1.Node.js 连接 MySQL $ cnpm install mysql 连接mysql: var m ...