`

<<javascript the good parts>> variable scope

 
阅读更多

Javascript does not have block scope, but it does have function scope. A variable defined anywhere within a function is visible everywhere within the function.

 

function foo(){
  console.log("a:",a);//variable a is visible here but it is still undefined
  console.log("f:",f);//variable f is visible here but it is still undefined
  var a=1;
  var f=function(){
    console.log("f");
  }
}

foo();

 

So, it is best to declare all of the variables used in a function at the top of the function body. If we check the angularjs source code, we find it does exactly the same way.

// at the top, it declares all the global variables
var $$scope           = '$scope',
    $boolean          = 'boolean',
    $console          = 'console',
    $length           = 'length',
    $name             = 'name',
    $object           = 'object',
...

// in each function, it also declare the variables at the top 
function forEach(obj, iterator, context) {
  var key;
  if (obj) {...
...
 

 

 

分享到:
评论

相关推荐

    JavaScript the good parts 经典课程

    "JavaScript the Good Parts"是一本由Douglas Crockford编写的经典书籍,它深入浅出地讲解了JavaScript的精华部分,帮助开发者理解并掌握这门语言的精髓。在这个经典课程中,我们将探讨以下几个关键知识点: 1. **...

    网页教程代码参考

    &lt;br&gt;HTML&lt;br&gt;HTML教程&lt;br&gt;&lt;br&gt;HTML代码示例&lt;br&gt;&lt;br&gt;HTML参考(Reference)&lt;br&gt;&lt;br&gt;HTML知识库&lt;br&gt;&lt;br&gt;CSS&lt;br&gt;CSS教程&lt;br&gt;&lt;br&gt;CSS代码示例&lt;br&gt;&lt;br&gt;CSS参考(Reference)&lt;br&gt;&lt;br&gt;Javascript&lt;br&gt;Javascript教程&lt;br&gt;&lt;br&gt;...

    JavaScript the good parts

    《JavaScript the Good Parts》是Douglas Crockford撰写的一本权威书籍,专注于揭示JavaScript语言中优秀的部分,旨在帮助开发者在编程实践中避开语言的缺陷,提高代码的可读性、可靠性和可维护性。这本书是在...

    JavaScript: The Good Parts

    《JavaScript: The Good Parts》&lt;br&gt;by Douglas Crockford&lt;br&gt;Publisher: O'Reilly &lt;br&gt;Pub Date: May 2, 2008 &lt;br&gt;Print ISBN-13: 978-0-596-51774-8 &lt;br&gt;Pages: 170

    JavaScript.The.Good.Parts

    《JavaScript.The.Good.Parts》是一本深受程序员喜爱的经典JavaScript指南,由著名的计算机科学家Douglas Crockford撰写。这本书深入浅出地介绍了JavaScript的核心概念、语法特性以及最佳实践,旨在帮助开发者掌握...

    元旦倒计时JavaScript代码

    &lt;div class="djs"&gt; &lt;p&gt;元旦倒计时:&lt;/p&gt; &lt;div class="time"&gt; &lt;span class="day"&gt;0&lt;/span&gt; &lt;span&gt;天&lt;/span&gt; &lt;span class="hour"&gt;1&lt;/span&gt; &lt;span&gt;小时&lt;/span&gt; &lt;span class="minute"&gt;2&lt;/span&gt; &lt;span&gt;分&lt;/span&gt; ...

    js星级评论打分效果

    &lt;span&gt;js星级评论打分&lt;/span&gt; &lt;ul&gt; &lt;li&gt; &lt;a href="javascript:;"&gt;1&lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="javascript:;"&gt;2&lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="javascript:;"&gt;3&lt;/a&gt; &lt;/li&gt; ...

    HTML&JavaScript经典PPT 讲的都是重点

    &lt;SCRIPT language="JavaScript" &gt; function compute(op) { var num1,num2; num1=parseFloat(document.myform.num1.value); num2=parseFloat(document.myform.num2.value); if (op=="+") document.myform....

    JSP图书管理系统

    &lt;script language="JavaScript"&gt;var when=new Date(); document.write("&lt;h4&gt;"); document.write(when.getYear()+"年"); document.write((when.getMonth()+1)+"月"); document.write(when.getDate()+...

    手机信息的增删改查,分页查询

    手机信息的增删改查,分页查询。 &lt;%@ page language="java" import="java.util.*" pageEncoding=...&lt;script type="text/javascript"&gt; &lt;!-- function delok() { return confirm("确定要删除吗?"); } //--&gt; &lt;/script&gt;

    jsf12-maven.zip

    &lt;scope&gt;compile&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.faces&lt;/groupId&gt; &lt;artifactId&gt;jsf-impl&lt;/artifactId&gt; &lt;version&gt;1.2_13&lt;/version&gt; &lt;scope&gt;runtime&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; ...

    java实用程序设计100例原代码和素材下载

    &lt;p&gt;&lt;span class="javascript" style="FONT-SIZE: 12px"&gt;&lt;font face="Verdana"&gt;1 图形用户界面&lt;br /&gt;2 applet编辑&lt;br /&gt;3 多媒体处理&lt;br /&gt;4 java输入输出系统&lt;br /&gt;5 java网络编程&lt;br /&gt;6 java数据库编程&lt;br /&gt;7 ...

    创建模态网页对话框(Very Good)!

    }&lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;form name=frm_test id=frm_test&gt;&lt;input type=button value=Click onclick=test()&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;The code for the modal.html which will be called when u click the &quot;...

Global site tag (gtag.js) - Google Analytics