在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的更多相关文章

  1. javascript作用域(Scope),简述上下文(context)和作用域的定义

    网页制作Webjx文章简介:这篇文章将正面解决这个问题:简述上下文(context)和作用域的定义,分析可以让我们掌控上下文的两种方法,最后深入一种高效的方案,它能有效解决我所碰到的90%的问题. 作 ...

  2. JavaScript作用域[[scope]]

    [[scope]] : 隐式的属性 每个JavaScript函数都是一个对象,对象中有些属性可以访问,而有些属性是不可以访问的,这些属性仅供JavaScript引擎存取, [[scope]]就是其中一 ...

  3. [Javascript] Function scope

    We have code like: var numbers = [1,2,3]; for(var i in numbers){ setTimeout(function(){console.log(n ...

  4. JavaScript闭包的底层运行机制

    转自:http://blog.leapoahead.com/2015/09/15/js-closure/ 我研究JavaScript闭包(closure)已经有一段时间了.我之前只是学会了如何使用它们 ...

  5. JAVASCRIPT的一些知识点梳理

    春节闲点,可以安心的梳理一下以前不是很清楚的东东.. 看的是以下几个URL: http://web.jobbole.com/82520/ http://blog.csdn.net/luoweifu/a ...

  6. JavaScript的语法要点 3 - Calling Context

    上一篇讲了JavaScript的Scope Chain - 每一个函数都有一个scope chain与之关联,scope chain上有第一个对象维护着本地变量作为其属性.另外我们在JavaScrip ...

  7. Scope Directive

    ---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...

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

  9. JavaScript 作用域链解析

    JavaScript 中有 Scope( 作用域 ) , Scope chain( 作用域链 ) , Execute context( 执行上下文 ) , Active Object ( 活动对象 ) ...

随机推荐

  1. 新萝卜家园Ghost版Win10系统X32极速装机版2015年4月

    来自:系统妈,系统下载地址:http://www.xitongma.com/windows10/2015-03-30/6638.html 新萝卜家园Ghost Win10 X32 10041电脑城极速 ...

  2. CSS3 自动旋转

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. VC++线程函数内怎么调用外部函数

    VC++线程函数内怎么调用外部函数 1.把外部函数做成静态函数,就可以直接调用了.2.把外部函数所在的对象通过线程函数参数传到线程里面来,这样线程里可以使用此对象及其函数了.

  4. Webstorm 的 Tab 键调整缩进值

    两步即可,注意版本

  5. 诊断:RHEL7安装11.2RAC时root.sh错误ohasd failed to start

    RHEL 7.5中安装11gRAC时,在grid infrastructure的root.sh执行时,报错: # /oracle/product/11g/grid/root.sh ... Adding ...

  6. 远程连接mysql数据库

    远程连接mysql数据库需要以下步骤(改表法): 1.数据源(数据库服务器)对要访问数据库进行授权: 2.本地库(本地)连接: 具体方法: 1. 进入数据服务器,输入cmd进入控制台:输入 mysql ...

  7. [Python3网络爬虫开发实战] 1.5.4-RedisDump的安装

    RedisDump是一个用于Redis数据导入/导出的工具,是基于Ruby实现的,所以要安装RedisDump,需要先安装Ruby. 1. 相关链接 GitHub:https://github.com ...

  8. MT4系统自带指标代码

    MT4系统自带指标代码 ~ Accelerator Oscillator 震荡加速指标:                   double iAC() ~ Accumulation/Distribut ...

  9. 杭电 2111 Saving HDU (贪心)

    Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的.   一天,当他正在苦思冥想解困良策的时候,突然想到了自己 ...

  10. <git>……git的基本使用……//

    1.切换到存放git版本库的地方 2.Git clone url(github上的地址) 3.设置全局用户(输入一次即可) git config --global user.name github上的 ...