JS not "block scope"

2010-03-17  来源:本站原创  分类:Web  人气:200 

No other meaning, just write it down only, which Rhino book is clearly and simply written for.

<script>
for(var i = 0; i < 10; i++){
    var a = i;
}
alert(a);//alert 9, Instead of undefined, or an error  ,a Being registered to a global variable
</script>

Slightly deeper look

<script>
try{
  alert(abcde);
}catch(e){
  alert(e);
}// It gives me errors
try{
  alert(a);
}catch(e){
  alert(e);
}//alert undefined, Instead of an error, ditto
for(var i = 0; i < 10; i++){
    var a = i;
}
</script>

Again a

<script>
var s=2;
function test(){
   alert(s);//undefined
   var s=1;
   alert(a);//undefined
   if(s===3){
      var a=2;
   }
   alert(a);//undefined
}
test();
alert(s)//2, Looking for from the global variable  s
</script>

The case of the test () is equivalent to

function test(){
  var s;
  var a;
  alert(s);
  s=1;
  alert(a);
  if(s===3){
    a=2;
  }
  alert(a);
}
相关文章
  • JS not "block scope" 2010-03-17

    No other meaning, just write it down only, which Rhino book is clearly and simply written for. <script> for(var i = 0; i < 10; i++){ var a = i; } alert(a);//alert 9, Instead of undefined, or an error ,a Being registered to a global variable </

  • js inferred variable scope 2010-12-17

    Originally written in the post in, but felt should keep one end, put it, huh, huh. Forum posts Location: http://www.javaeye.com/topic/842542 This question has troubled me for a long time before. Now finally want to understand, especially to share, if

  • [Change] Js in the scope and createDelegate () 2010-09-16

    The use of JavaScript in this regard, it is a long-standing problem. We are not here to introduce its history, and only with concrete examples, tell us what problems you may experience, in the face of these problems is how to solve EXT. When using th

  • The scope of JavaScript variables do not block 2010-10-31

    Coverage of local variables global variables, as follows: <script> // Define global variables test var test = " Global variables "; // Defined function checkscope function checkscope() { // Definition of local variables // var test = "

  • javascript函数作用域学习示例(js作用域) 2013-12-02

    javascript中没有块级作用域,取而代之的javascript使用的是函数作用域,下面使用示例学习一下js作用域的使用方法 在一些类似c语言的编程语言中,花括号内的每一段代码都具有各自的作用域,而且变量在声明他们的代码段之外是不可见的,我们称为块级作用域(block scope),而javascript中没有块级作用域.取而代之的javascript使用的是函数作用域(function scope):变量在声明它的函数体以及这个函数体嵌套的任意函数体内都是有定义的. 在如下代码中,在不同位

  • JS的函数(Functions) 2014-07-16

    JS的函数(Functions) Parameters js的函数可以接受多个参数 function sumOnSteroids() { var i, res = 0, number_of_params = arguments.length; for (i = 0; i < number_of_params; i++) { res += arguments[i]; } return res; } console.log(sumOnSteroids(1, 2, 43));//46 console.

  • Javascript Scope Analysis 2010-02-11

    To learn any programming language, can not escape the scope of understanding of the variable. In javascript, variable scope with the global (window object) scope and function call scope. The following variables have global scope 1. All the outermost

  • High-performance web development - how to load JS, JS should be placed in what position? [Change] 2010-04-12

    Download blocking external JS http://www.blogjava.net/BearRui/archive/2010/04/08/web_performance_js_where.html JS browser to download all the time, would prevent all other activities, such as other resources, downloads, content presentation and so on

  • C stored in the class, links, scope 2010-07-06

    C stored in the class, links, scope C, variable according to different standards have different classifications, generally three major categories: storage period, scope, links. Scope: Scope describes the program can access an identifier of one or mor

  • js mass participation, background garbled 2010-09-29

    Js code block in front of each variable encoding process is as follows: var typeselect = form2.typeselect.value; / / Var typeselect = document.getElementById ('typeselect'). Value; / / How to obtain their own way should not necessarily have to like m

  • AngularJS 的 scope.$apply 2013-12-25

    如果你写过angular程序,肯定对scope.$apply不会陌生,表面上,他的作用就是把改变同步绑定到界面上.但是它为什么存在呢?我们什么时候需要用到它呢?什么时候不需要呢? 要真正理解$apply, 就必须知道我们为什么需要它. 首先,javascript是单线程执行的,我们写的javascript代码不是一口气就执行完的,而且是很多周期中完成的.每一个周期从开始到结束是不会被中断的,当浏览器主线程的一个周期在执行一段代码时,其他的代码就不会执行,UI渲染进程也会被挂起,浏览器就不会干被的

  • Angularjs中的directive在声明scope时的修饰符及其用法 2014-05-20

    在我们自定义directive时,一般的格式如下: <!-- lang: js --> return { restrict: 'AE', scope: {}, template: '<div></div>', link: function() {} } 除了代码中出现的属性,还有一些其他的属性可供配置,这里不作详述. 今天我们要说的重点是scope字段. 常规用法 scope的常规设置如下: <!-- lang: js --> scope: { name:

  • angular.js控制器之间的通信 2014-10-09

    环境:angular.js+sastruts+apache-tomcat 学习前端的一个设计工具angular.js的过程中会遇到这样一个问题:angular.js控制器之间的通信. 查看了一些资料,大体有以下的解决方案. 1.使用$on,$emit,$boardcast这几个方式来实现,其中$on表示事件监听,$emit表示向父级以上的作用域触发事件,$boardcast表示向子级以下的作用域广播事件.即子控制器中的变量通过$emit向父级控制器传播,父级控制器中通过$on事件监听,并使用$b

  • AngularJS+Satellizer+Node.js+MongoDB- Instagram-16 2015-02-02

    Build an Instagram clone with AngularJS, Satellizer, Node.js and MongoDB 16.回到客户端 让我们创建一个新用户,然后登陆.验证一下我们后台功能的实现是不是像预期一样可以开始工作了. 刷新浏览器,从顶部导航点击注册连接,填写注册资料然后点击注册按钮. 如果一切正常的话你应该会被重定向到这个页面: 怎么回事?好的,默认的, Satellizer 会在用户注册成功之后自动登陆.你如果愿意你可以在 app.js 里面把它关掉: <

  • Javascript Closure 2009-04-22

    Translation Original: http://blog.csdn.net/Ant_Yan/archive/2008/09/24/2972572.aspx English Original: http://jibbering.com/faq/faq_notes/closures.html Closure Introduction Js closure is a closure of the code block (it is generally a function), it cont

  • javascript object-oriented technology infrastructure (VI) (reproduced) 2010-10-14

    Original: javascript object-oriented technology infrastructure (VI) Scope, closures, analog private property first to mention briefly about the variable scope, we are familiar with these things, so no details. Js code var sco = "global"; / / glo

  • JavaScript development of norms 2010-12-17

    Dinner from the front of the original specification Taobao: http://kissyteam.github.com/docs/html/styleguide/js-style-rules.html Line and indented Statement line Do not make more than 120 characters per line; Statement must be a semicolon as a termin

  • javascript变量声明实例分析 2014-05-27

    这篇文章主要介绍了javascript变量声明,实例分析了javascript变量声明的相关使用技巧,需要的朋友可以参考下 本文实例讲述了javascript变量声明的方法.分享给大家供大家参考.具体分析如下: js中使用一个变量之前应当先声明.变量使用关键字var来声明. 如果未在var声明语句中给变量指定初始值,则该变量值为undefined. 不用在声明变量时指定变量类型,js变量可以是任意数据类型. 使用var语句重复声明变量是合法且无害的.如果重复声明带有初始化器,则就和简单的赋值语句

  • JavaScript中的变量作用域介绍 2014-12-27

    这篇文章主要介绍了JavaScript中的变量作用域介绍,本文同时讲解了一个新概念变量的作用域链,需要的朋友可以参考下 对于变量的作用域(scope),C.Java等语言采取的是"block scope"的方式.与之不同,JavaScript所采取的是"function scope"的方式 - 变量的作用域仅由所处的function决定,与if.for等逻辑块无关.比如,以下这个例子展示了JavaScript中与C.Java等语言不一样的行为: function()

  • Explore the javascript functions in the implementation of the order of 2009-01-04

    Reprinted from: http://blog.csdn.net/tenfyguo/archive/2009/09/06/4525957.aspx First-come, first paragraph of the test code, as follows: Test code 1: <script language="JavaScript"> "! -- function myFn () ( alert ( 'Fn1'); ); myFn (); f