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"是一本由Douglas Crockford编写的经典书籍,它深入浅出地讲解了JavaScript的精华部分,帮助开发者理解并掌握这门语言的精髓。在这个经典课程中,我们将探讨以下几个关键知识点: 1. **...
<br>HTML<br>HTML教程<br><br>HTML代码示例<br><br>HTML参考(Reference)<br><br>HTML知识库<br><br>CSS<br>CSS教程<br><br>CSS代码示例<br><br>CSS参考(Reference)<br><br>Javascript<br>Javascript教程<br><br>...
《JavaScript the Good Parts》是Douglas Crockford撰写的一本权威书籍,专注于揭示JavaScript语言中优秀的部分,旨在帮助开发者在编程实践中避开语言的缺陷,提高代码的可读性、可靠性和可维护性。这本书是在...
《JavaScript: The Good Parts》<br>by Douglas Crockford<br>Publisher: O'Reilly <br>Pub Date: May 2, 2008 <br>Print ISBN-13: 978-0-596-51774-8 <br>Pages: 170
《JavaScript.The.Good.Parts》是一本深受程序员喜爱的经典JavaScript指南,由著名的计算机科学家Douglas Crockford撰写。这本书深入浅出地介绍了JavaScript的核心概念、语法特性以及最佳实践,旨在帮助开发者掌握...
<div class="djs"> <p>元旦倒计时:</p> <div class="time"> <span class="day">0</span> <span>天</span> <span class="hour">1</span> <span>小时</span> <span class="minute">2</span> <span>分</span> ...
<span>js星级评论打分</span> <ul> <li> <a href="javascript:;">1</a> </li> <li> <a href="javascript:;">2</a> </li> <li> <a href="javascript:;">3</a> </li> ...
<SCRIPT language="JavaScript" > function compute(op) { var num1,num2; num1=parseFloat(document.myform.num1.value); num2=parseFloat(document.myform.num2.value); if (op=="+") document.myform....
<script language="JavaScript">var when=new Date(); document.write("<h4>"); document.write(when.getYear()+"年"); document.write((when.getMonth()+1)+"月"); document.write(when.getDate()+...
手机信息的增删改查,分页查询。 <%@ page language="java" import="java.util.*" pageEncoding=...<script type="text/javascript"> <!-- function delok() { return confirm("确定要删除吗?"); } //--> </script>
<scope>compile</scope> </dependency> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-impl</artifactId> <version>1.2_13</version> <scope>runtime</scope> </dependency> <dependency> ...
<p><span class="javascript" style="FONT-SIZE: 12px"><font face="Verdana">1 图形用户界面<br />2 applet编辑<br />3 多媒体处理<br />4 java输入输出系统<br />5 java网络编程<br />6 java数据库编程<br />7 ...
}</script></head><body><form name=frm_test id=frm_test><input type=button value=Click onclick=test()></form></body></html>The code for the modal.html which will be called when u click the "...