一 概述

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 )的更多相关文章

  1. JavaScript中变量提升------Hoisting

    原谅链接:http://www.cnblogs.com/damonlan/archive/2012/07/01/2553425.html 因为这个问题很是经典,而且容易出错,所以在介绍一次.哈哈.莫怪 ...

  2. js中的变量提升(hoisting)

    来看如下代码: function HelloJS(){ var array = [1,2,3,4,5]; for(var i in array){ } alert(i); } HelloJS(); a ...

  3. JS中作用域和变量提升(hoisting)的深入理解

    作用域(Scoping) javascript作用域之所以迷惑,是因为它程序语法本身长的像C家族的语言.我对作用域的理解是只会对某个范围产生作用,而不会对外产生影响的封闭空间.在这样的一些空间里,外部 ...

  4. js函数、变量提升(hoisting)

    其实我只是想复习下变量提升的,然后看到了函数提升,然后再看到了函数声明.函数表达式. 有必要怀着敬仰之心提及园子里的TOM大叔的解密命名函数表达式,不愧是大叔,好好地脑补了下基础知识. 在ECMASc ...

  5. javascript变量提升详解

    js变量提升 对于大多数js开发者来说,变量提升可以说是一个非常常见的问题,但是可能很多人对其不是特别的了解.所以在此,我想来讲一讲. 先从一个简单的例子来入门: a = 2; var a; cons ...

  6. JavaScript变量提升和函数声明预解析

    1.首先理解函数作用域 在JavaScript中,变量的定义并不是以代码块作为作用域的,而是以函数作用作用域的.也就是说,如果变量是在某个函数中定义的,那么它在函数以外的地方是不可见的.而如果该变量是 ...

  7. JavaScript中的各种变量提升(Hoisting)

    首先纠正下,文章标题里的 “变量提升” 名词是随大流叫法,“变量提升” 改为 “标识符提升” 更准确.因为变量一般指使用 var 声明的标识符,JS 里使用 function 声明的标识符也存在提升( ...

  8. 什么是 js 变量提升 (Javascript Hoisting)

    Javascript是一门容易遭人误解的语言,但是它的强大毋庸置疑.个人觉得,要想深入理解Javascript语言,首先必须对其基本的概念(例如:Scope,Closure,Hoisting等)要真正 ...

  9. js 变量提升(JavaScript Scoping and Hoisting)

    原文网址:http://www.cnblogs.com/betarabbit/archive/2012/01/28/2330446.html 这是一篇作者翻译过来的文章,未翻译的原文网址在这里:htt ...

随机推荐

  1. 通过Sonar的代码质量报告学习【如何写安全高质量的代码】

    1.不要用.size(),改用isEmpty() Using Collection.size() to test for emptiness works, but using Collection.i ...

  2. 深度学习原理与框架-神经网络-cifar10分类(代码) 1.np.concatenate(进行数据串接) 2.np.hstack(将数据横着排列) 3.hasattr(判断.py文件的函数是否存在) 4.reshape(维度重构) 5.tanspose(维度位置变化) 6.pickle.load(f文件读入) 7.np.argmax(获得最大值索引) 8.np.maximum(阈值比较)

    横1. np.concatenate(list, axis=0) 将数据进行串接,这里主要是可以将列表进行x轴获得y轴的串接 参数说明:list表示需要串接的列表,axis=0,表示从上到下进行串接 ...

  3. gitlab入门

    目录 git下载 注册ssh git客户端及图形化工具tortoisegit下载: git客户端安装: https://git-scm.com/download   (git客户端 此种需要命令行执行 ...

  4. 趣味编程:静夜思(Kotlin版)

    import java.util.* fun verticalWriting(txt:String, offset:Int) = txt.mapIndexed { i, c -> Pair(i, ...

  5. Python文件和目录模块介绍:glob、shutil、ConfigParser

    glob模块 查找符合特定规则的文件路径名,路径名可以使用绝对路径也可以使用相对路径.查找文件会使用到三个通配符,星号*,问号?和中括号[],其中"*"表示匹配0~n个字符, &q ...

  6. [Shell]Shell脚本的执行方式

    ---------------------------------------------------------------------------------------------------- ...

  7. 让一个非窗口组件(non-windowed component)可以接受来自Windows的消息

    为什么要这样做? 有时候我们需要一个非窗口组件(比如一个非继承自TWinContrl的组件)可以接受Windows消息.要接受消息就需要一个窗口句柄,但是非窗口组件却没有句柄.这篇文章将讲述怎么让一个 ...

  8. python函数基础:调用内置函数&定义函数

    调用内置函数 有很多内置函数,在使用中需要积累.这里只举两个例子: 分别调用abs和数据类型转换,注意当入参类型错误时候会报错 ''' print('abs(-100)') abs(-100) pri ...

  9. 0 开发的准备工作一一虚拟机virturalbox

    https://www.virtualbox.org/wiki/Linux_Downloads官网下载linux版本 https://www.ubuntu.com/desktop/developers ...

  10. Hibernate 再接触 树状结构设计以及学生课程成绩表的设计

    1 树状结构的设计 package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax. ...