JavaScript’s function scope means that all variables declared within a function are visi-
ble throughout the body of the function. Curiously, this means that variables are even
visible before they are declared. This feature of JavaScript is informally known as hoist-
ing: JavaScript code behaves as if all variable declarations in a function (but not any
associated assignments) are “hoisted” to the top of the function. Consider the following
code:

  1. var scope = "global";
  2. function f() {
  3. console.log(scope); // Prints "undefined", not "global"
  4. var scope = "local"; // Variable initialized here, but defined everywhere
  5. console.log(scope); // Prints "local"
  6. }

You might think that the first line of the function would print “global”, because the
var statement declaring the local variable has not yet been executed. Because of the
rules of function scope, however, this is not what happens. The local variable is defined
throughout the body of the function, which means the global variable by the same name
is hidden throughout the function. Although the local variable is defined throughout,
it is not actually initialized until the var statement is executed. Thus, the function above
is equivalent to the following, in which the variable declaration is “hoisted” to the top
and the variable initialization is left where it is:

  1. function f() {
  2. var scope; //Local variable is declared at the top of the function
  3. console.log(scope);//It exists here, but still has "undefined" value
  4. scope = "local";//Now we initialize it and give it a value
  5. console.log(scope);//And here it has the value we expect
  6. }

Function Scope的更多相关文章

  1. [Javascript] Function scope

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

  2. [Javascript] Advanced Function Scope

    Something like 'for' or 'while', 'if', they don't create a new scope: ,,]; ; i < ary.length; i++) ...

  3. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  4. scope.$apply是干嘛的

    开始用angular做项目的时候,一定碰到过$scope.$apply()方法,表面上看,这像是一个帮助你进行数据更新的方法,那么,它为何存在,我们又该如何使用它呢. JavaScript执行顺序 J ...

  5. (转)构建自己的AngularJS,第一部分:Scope和Digest

    原翻译链接:https://github.com/xufei/Make-Your-Own-AngularJS/edit/master/01.md 原文链接:http://teropa.info/blo ...

  6. AngularJS Scope(作用域)

    1. AngularJS Scope(作用域) Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. Sc ...

  7. angularjs $scope.$watch(),监听值得变化

    myApp.controller('firstController',function($scope,$interval){ $scope.date = new Date(); setInterval ...

  8. angularjs $scope.$apply 方法详解

    myApp.controller('firstController',function($scope,$interval){ $scope.date = new Date(); setInterval ...

  9. angularjs中的directive scope配置

    angularjs中的directive scope配置 定义directive其中重要的一环就是定义scope,scope有三种形式: 默认的scope,DOM元素上原有的scope scope: ...

随机推荐

  1. C++使用throw抛出异常

    引用:    c++ 使用throw抛出异常 抛出异常(也称为抛弃异常)即检测是否产生异常,在C++中,其采用throw语句来实现,如果检测到产生异常,则抛出异常.该语句的格式为:throw 表达式; ...

  2. css选择器集体声明

    <title>静夜思</title><style type="text/css">h1,#two,.red{ color:#ff0000; fo ...

  3. iOS 查看系统字体效果的网页

    常常需要查看字体的样式,这里推荐一个网页http://iosfonts.com,方便查阅.

  4. Light OJ 1253 Misere Nim (尼姆博弈(2))

    LightOJ1253 :Misere Nim 时间限制:1000MS    内存限制:32768KByte   64位IO格式:%lld & %llu 描述 Alice and Bob ar ...

  5. 【Python爬虫】入门知识

    爬虫基本知识 这阵子需要用爬虫做点事情,于是系统的学习了一下python爬虫,觉得还挺有意思的,比我想象中的能干更多的事情,这里记录下学习的经历. 网上有关爬虫的资料特别多,写的都挺复杂的,我这里不打 ...

  6. android 获取资源文件 r.drawable中的图片转换为drawable、bitmap

    1.R-Drawable Resources resources = mContext.getResources(); Drawable drawable = resources.getDrawabl ...

  7. SecureCRT乱码

    http://jingyan.baidu.com/article/948f59245be128d80ff5f9aa.html

  8. ThinkPHP的增、删、改、查

    一.创建操作 在ThinkPHP使用add方法新增数据到数据库. 使用方法如下: $User = M()->; $data[););')->delete();

  9. poj 3281 最大流+建图

    很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很 ...

  10. Office Word 2013发布带数学公式的博客

    今日在自学冈萨雷斯的<数字图像处理>一书,想做笔记来记录,并分享一些MATLAB代码,又加上刚刚注册博客园的账户,便想着如何能够较为方便的来书写博客.本文主要涉及到的问题有: 如何用wor ...