在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.

最新文章

  1. 攻城狮在路上(陆)-- 配置hadoop本地windows运行MapReduce程序环境
  2. 分离与继承的思想实现图片上传后的预览功能:ImageUploadView
  3. XML通过XSL格式化的那点事(XML到自定义节点折叠显示)
  4. 忘记常访问网站密码怎么办?教你如何查看浏览器已保存的密码,如何简单查看Chome浏览器保存的密码?
  5. 【pom.xml 依赖】使用net.sf.json-lib-2.4-jdk15.jar所需要的其他依赖架包 以及其一直在pom.xml报错的问题
  6. JDK源码调试
  7. Spark RDD概念学习系列之RDD的5大特点(五)
  8. Linux系统编程——进程间通信:命名管道(FIFO)
  9. bzoj2733
  10. <s:iterator> 序号
  11. 安装Theano
  12. Ubuntu 创建启动器
  13. python实现希尔排序(已编程实现)
  14. linux上部署JMeter
  15. eclipse如何忽略、过滤不需要提交到svn的文件和目录
  16. Java高级特性 第13节 解析XML文档(1) - DOM和XPath技术
  17. 论文笔记:Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks
  18. 设计模式之责任链模式(Chain of Responsibility )
  19. db.properties文件的配置格式
  20. crm 中使用的Django方法

热门文章

  1. [6818开发板]八核开发板|4G开发板|GPS开发板|嵌入式开发平台
  2. lua之链表的实现
  3. 【JavaScript从入门到精通】第二课
  4. 2.10.2 section元素
  5. mysql通配符进行模糊查询
  6. Python 1-3区分Python文件的两种用途和模块的搜索路径
  7. mysql explain的使用
  8. Jquery validate自定义验证
  9. ASP.NET MVC的帮助类HtmlHelper和UrlHelper
  10. jQuery_DOM学习之------创建节点及节点属性