ES3之变量提升 ( hoisting )
一 概述
JavaScript引擎在预编译时,会将声明(函数声明、变量声明)自动提升至函数或全局代码的顶部。但是赋值不会提升。
Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code.
It's important to point out that the hoisting will affect the variable declaration, but not its value's initialization. The value will be indeed assigned when the assignment statement is reached:
二 var声明的变量,在非严格模式、严格模式都会提升
例一 非严格模式
function hoisting(){
// 如果变量未提升,会报错 Uncaught ReferenceError: saint is not defined
console.log(saint);
var saint = 'Aioria';
console.log(saint);
}
hoisting();

例二 严格模式
var saint = 'Orpheus';
function hoisting(){
console.log(saint);
var saint = 'Aioria';
console.log(saint);
}
hoisting();

例三 函数
function test(){
console.log(name,hello,welcome);
var name = 'Tom';
var hello = function(){
console.log('hello~');
};
function welcome(){
console.log('welcome~');
}
hello();
welcome();
}
test();

三 let声明的变量,在非严格模式、严格模式都不会提升。
function hoisting(){
console.log(saint);
let saint = 'Aioria';
console.log(saint);
}
hoisting();


ES3之变量提升 ( hoisting )的更多相关文章
- JavaScript中变量提升------Hoisting
原谅链接:http://www.cnblogs.com/damonlan/archive/2012/07/01/2553425.html 因为这个问题很是经典,而且容易出错,所以在介绍一次.哈哈.莫怪 ...
- js中的变量提升(hoisting)
来看如下代码: function HelloJS(){ var array = [1,2,3,4,5]; for(var i in array){ } alert(i); } HelloJS(); a ...
- JS中作用域和变量提升(hoisting)的深入理解
作用域(Scoping) javascript作用域之所以迷惑,是因为它程序语法本身长的像C家族的语言.我对作用域的理解是只会对某个范围产生作用,而不会对外产生影响的封闭空间.在这样的一些空间里,外部 ...
- js函数、变量提升(hoisting)
其实我只是想复习下变量提升的,然后看到了函数提升,然后再看到了函数声明.函数表达式. 有必要怀着敬仰之心提及园子里的TOM大叔的解密命名函数表达式,不愧是大叔,好好地脑补了下基础知识. 在ECMASc ...
- javascript变量提升详解
js变量提升 对于大多数js开发者来说,变量提升可以说是一个非常常见的问题,但是可能很多人对其不是特别的了解.所以在此,我想来讲一讲. 先从一个简单的例子来入门: a = 2; var a; cons ...
- JavaScript变量提升和函数声明预解析
1.首先理解函数作用域 在JavaScript中,变量的定义并不是以代码块作为作用域的,而是以函数作用作用域的.也就是说,如果变量是在某个函数中定义的,那么它在函数以外的地方是不可见的.而如果该变量是 ...
- JavaScript中的各种变量提升(Hoisting)
首先纠正下,文章标题里的 “变量提升” 名词是随大流叫法,“变量提升” 改为 “标识符提升” 更准确.因为变量一般指使用 var 声明的标识符,JS 里使用 function 声明的标识符也存在提升( ...
- 什么是 js 变量提升 (Javascript Hoisting)
Javascript是一门容易遭人误解的语言,但是它的强大毋庸置疑.个人觉得,要想深入理解Javascript语言,首先必须对其基本的概念(例如:Scope,Closure,Hoisting等)要真正 ...
- js 变量提升(JavaScript Scoping and Hoisting)
原文网址:http://www.cnblogs.com/betarabbit/archive/2012/01/28/2330446.html 这是一篇作者翻译过来的文章,未翻译的原文网址在这里:htt ...
随机推荐
- offsetof使用小结
先上例子 #include <stdio.h> #include <stdlib.h> /* offsetof example */ #include <stddef.h ...
- RADIDE MultiPaste
RADIDE MultiPaste https://community.embarcadero.com/blogs/entry/multipaste-in-the-rad-studio-ide htt ...
- office2016系列产品关闭时卡顿
关闭Print Spooler服务其方法如下: win+r键–>输入services.msc点击确定如下图 单击word中的文件-选项-加载项-COM加载项-转到,将所有加载项前面的勾都去掉(不 ...
- day02-格式化输出
python格式化字符串有%和{}两种 字符串格式控制符. 字符串输入数据格式类型(%格式操作符号) %%百分号标记%c字符及其ASCII码%s字符串%d有符号整数(十进制)%u无符号整数(十进制)% ...
- git 和 github 学习总结
https://mp.weixin.qq.com/s?src=11×tamp=1543302553&ver=1269&signature=NAX65qusuVVDEl ...
- intellij idea远程debug调试resin4教程
昨天有个项目部署在阿里云 想远程调试不知道怎么弄.看日志需要账户密码很不方便呀.今天加班特意baidu了下. 1.先在远程的resin修改conf中resin.xml配置文件 在server-defa ...
- a stop job is running for Security Auditing Services
内核是3.10.0-514.el7,启动时有如下报错: a stop job is running for Security Auditing Services(56s / 1min 30s) 系统启 ...
- How to Pronounce the Word OR
How to Pronounce the Word OR Share Tweet Share Tagged With: OR Reduction Study the OR reduction. Th ...
- Android 开发进入Linux系统执行命令 2018-5-25 Fri.
/** * 进入linux cmd执行命令 * * @param command * @return */ private boolean runRootCommand(String command) ...
- 当点击回车键后form表单就可提交的实现
$('#myform').find('input').on('keyup',function(event){ if(event.keyCode == 13){ $('#myform').submit( ...