<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>What is hoisting in Javascript?</title>
<style type="text/css">
</style>
<script type="text/javascript">
var a = 1;
function b() {
alert("step 1 a = " + a);
a = 10; // step 2
alert("step 2 a = " + a);
return;
function a() {} // step 1
}
b();
alert(a);
</script>
</head>
<body>
</body>
</html>
以上代码的执行顺序是;
1、The global a is set to 1
2、b() is called
3、function a() {} is hoisted and creates a local variable a that masks the global a
4、The local a is set to 10 (overwriting the function a)
5、The global a (still 1) is alerted
function b alert order:
===》1. step 1 a = function a() {}
===》2. step 2 a = 10
===》3. 1
Java 语言没有 Hoisting 的概念,看如下代码:
public class JavaHoistingTest {
public String myvar = "my value";
public void testHoisting(){
System.out.println(myvar); //output ---> my value is different with javascript undefined. 输出的是全局的变量的值。
String myvar = "my valued"; // and this line tips : The local variable myvar is never read. 定义的变量没有使用。
}
public static void main(String[] args) {
JavaHoistingTest hoisting = new JavaHoistingTest();
hoisting.testHoisting();
}
}
输出
my value
参考资料:
http://stackoverflow.com/questions/15311158/javascript-hoisting
http://hszy00232.blog.163.com/blog/static/43022753201131315817814/
http://www.programmerinterview.com/index.php/javascript/javascript-hoisting/
http://docstore.mik.ua/orelly/webprog/jscript/ch04_03.htm
分享到:
相关推荐
大家应该都只奥javascript的变量声明具有hoisting机制,JavaScript引擎在执行的时候,会把...网上关于JavaScript的变量声明提升问题的文章有很多,这篇文章将再次谈谈关于这方面的问题,有需要的朋友们可以参考借鉴。
本文主要给大家介绍了关于JavaScript中Hoisting(变量提升与函数声明提升)的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 如何将 函数声明 / 变量 “移动” 到作用域的顶部。 术语 ...
~~ [翻译]JavaScript Scoping and Hoisting 希望得到大家谅解。 一。案发现场 我们先看一段很简单的代码: 代码如下: var v=’Hello World’; alert(v); 这个没有疑问吧,弹出“Hello World”。OK,我们继续...
主要介绍了详解javascript 变量提升(Hoisting),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
javascript的变量声明具有hoisting机制,JavaScript引擎在执行的时候,会把所有变量的声明都提升到当前作用域的最前面。 先看一段代码 var v = "hello"; (function(){ console.log(v); var v = "world"; })(); ...
相信大家也都发现了,在网上关于JS的变量和作用域的文章有很多,但真正能讲清楚,能深入理解的文章很少。在阅读了很多人的文章以后,我决定综合起来,结合实际代码,希望能够以一个比较清楚完整的方式让大家真正理解...
刚接触到JavaScript的时候,便知道JavaScript是按顺序执行的,是如浏览器的解析DOM树一样的流程,解析DOM结构的时候,如果遇到JS脚本或者外联脚本便会停止解析,继续下载脚本之后,执行脚本,然后再解析DOM。...
翻译:可视化的 JavaScript:提升点击访问:原文地址作者:Lydia Hallie提升(Hoisting)是每个 JavaScript 开发人员都听说过
JavaScript提升机正确的迷你系列 成为刺客等级 集数: javascript葫芦常见问题及快速解答 JS引擎的工作方式Prepration(Parsing/Compilation),Execution JavaScript的基本概念: Hoisting, Scope, lookup, ...
JavaScript有一个精心设计的技巧,通常会导致麻烦(如今,即使技巧使用得当,它也会使代码的可读性降低)。 这是要点: // When we declare and assign a variable with `var` var myVariable = 1 ; // JavaScript ...
变量提升Hoisting是人们对JavaScript执行上下文工作方式的一种认识,并不是官方给出的改变 从字面上理解,变量提升的意思是变量和函数的声明会在物理层移动到作用域的最前面。但是这样理解并不准确,效果是相同的,...
JavaScript提升是将所有声明提升到当前作用域顶部的默认行为,英文叫Hoisting。 在JavaScript中,可以在使用变量之后对其进行声明。 也就是说,可以在声明变量之前使用它。比如: x = 5; document.getElementById...
JavaScript有一个精心设计的技巧,通常会导致麻烦(如今,即使技巧使用得当,它也会使代码的可读性降低)。 这是要点: // When we declare and assign a variable with `var` var myVariable = 1 ; // JavaScript ...