Variable hoisting Function hoisting
Variable hoisting
Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted to the top of the function or statement. However, variables that are hoisted will return a value of undefined. So even if you declare and initialize after you use or refer to this variable, it will still return undefined.
/**
* Example 1
*/
console.log(x === undefined); // true
var x = 3;
/**
* Example 2
*/
// will return a value of undefined
var myvar = "my value";
(function() {
console.log(myvar); // undefined
var myvar = "local value";
})();
The above examples will be interpreted the same as:
/**
* Example 1
*/
var x;
console.log(x === undefined); // true
x = 3;
/**
* Example 2
*/
var myvar = "my value";
(function() {
var myvar;
console.log(myvar); // undefined
myvar = "local value";
})();
Because of hoisting, all var statements in a function should be placed as near to the top of the function as possible. This best practice increases the clarity of the code.
In ECMAScript 2015, let (const) will not hoist the variable to the top of the block. However, referencing the variable in the block before the variable declaration results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.
console.log(x); // ReferenceError
let x = 3;
Function hoisting
For functions, only function declaration gets hoisted to the top and not the function expression.
/* Function declaration */
foo(); // "bar"
function foo() {
console.log("bar");
}
/* Function expression */
baz(); // TypeError: baz is not a function
var baz = function() {
console.log("bar2");
};
Variable hoisting Function hoisting的更多相关文章
- [JS] Topic - variable and function hoisting
Ref: 深入理解js的变量提升和函数提升 一.变量提升 简直就是es5的遗毒! console.log(global); // undefined 竟然能打印?因为变量提升,下一行就有定义 var ...
- learning scala How To Create Variable Argument Function - varargs :_ *
Scala collection such as List or Sequence or even an Array to variable argument function using the s ...
- CMake语法—普通变量与函数(Normal Variable And Function)
目录 CMake语法-普通变量与函数(Normal Variable And Function) 1 CMake普通变量与函数示例 1.1 CMakeLists.txt 1.2 执行CMake配置脚本 ...
- Linear regression with one variable - Cost function
摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第二章<单变量线性回归>中第7课时<代价函数>的视频原文字幕.为本人在视频学习过程中逐字逐句记录下 ...
- Linear regression with one variable - Cost function intuition I
摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第二章<单变量线性回归>中第8课时<代价函数的直观认识 - 1>的视频原文字幕.为本人在视频学习过 ...
- [Code::Blocks] Install wxWidgets & openCV
The open source, cross platform, free C++ IDE. Code::Blocks is a free C++ IDE built to meet the most ...
- 本人SW知识体系导航 - Programming menu
将感悟心得记于此,重启程序员模式. js, py, c++, java, php 融汇之全栈系列 [Full-stack] 快速上手开发 - React [Full-stack] 状态管理技巧 - R ...
- [JS] Topic - why "strict mode" here
Ref: Javascript 严格模式详解 使得Javascript在更严格的条件下运行: - 消除Javascript语法的一些不合理.不严谨之处,减少一些怪异行为; - 消除代码运行的一些不安全 ...
- ES3之变量提升 ( hoisting )
JavaScript引擎在预编译时,会将声明(函数声明.变量声明)自动提升至函数或全局代码的顶部.但是赋值不会提升. Because variable declarations (and declar ...
随机推荐
- PostgreSQL.conf文件配置详解[转]
一.连接配置与安全认证 1.连接Connection Settings listen_addresses (string) 这个参数只有在启动数据库时,才能被设置.它指定数据库用来监听客户端连接的 ...
- R语言:数据的分割-计算-整合(split-apply-aggregate)
当获取到原始数据时,我们通常的做法是对该数据进行分割成小片段,然后对各小片段进行计算统计,最后整合成最终的数据.这是统计学里数据处理的一般规律. R语言为我们提供了相应的函数来分别处理这三个阶段任务. ...
- How do I create zip file in Servlet for download?
原文链接:https://kodejava.org/how-do-i-create-zip-file-in-servlet-for-download/ The example below is a s ...
- INIT: vesion 2.88 booting
/***************************************************************************** * INIT: vesion 2.88 b ...
- Ubuntu16.04配置apache+php+mysql
命令行配置apache input sudo apt-get install apache2 done! 命令行配置mysql 参见: MySQL install and setting 命令行配置p ...
- LG4360 [CEOI2004]锯木厂选址
题意 原题来自:CEOI 2004 从山顶上到山底下沿着一条直线种植了 n 棵老树.当地的政府决定把他们砍下来.为了不浪费任何一棵木材,树被砍倒后要运送到锯木厂. 木材只能朝山下运.山脚下有一个锯木厂 ...
- .Net Remoting编程 ---- 系列文章
.Net Remoting(应用程序域) - Part.1 摘要: 本文是.Net Remoting系列的第一篇文章,讲述了Remoting的“前驱知识点”--应用程序域.传值封送(Marshal b ...
- springboot mybatisPlus配置
1.pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- C#将字符串数组转换为以逗号分隔的字符串
, tyt, gff }; string str=string.Join(",",array);
- HTTP-POST
POST方式:用来向目的服务器发出请求,要求它接受被附在请求后的实体,并把它当作请求队列中请求URI所指定资源的附加新子项,Post被设计成用统一的方法实现下列功能: 1:对现有资源的解释: 2:向电 ...