一 概述

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. requirements.txt 的使用与创建

    1. requirements.txt  主要是记录你的python 解释器安装了那些第三方模块,这样好方便项目迁移,自动解决掉项目的依赖关系 2. 网上找的那些关于 requirements 的文档 ...

  2. Centos 7 安装 sphinx2.2 (转)

    一.首先到Sphinx官网找到下载地址:http://sphinxsearch.com/downloads/release/如果你比较懒,好吧:http://sphinxsearch.com/file ...

  3. Python 百分号字符串拼接

    # %s可以接收一切 %d只能接收数字 msg = 'i am %s my hobby is %s' %('lhf','alex') print msg msg2 = 'i am %s my hobb ...

  4. [原]vue - webapp 返回无效 解决方案

  5. Hbase数据读写流程

    From: https://blog.csdn.net/wuxintdrh/article/details/69056188 写操作: Client写入,存入Memstore,Memstore满则Fl ...

  6. 各种Queue分析

    Queue主要方法的区别:   抛出异常 返回特殊值 插入 add(e)插入成功则返回true,没有可用空间则IllegalStateException offer(e) 移除 remove(e)获取 ...

  7. 2018SDIBT_国庆个人第六场

    A - A codeforces 714A Description Today an outstanding event is going to happen in the forest — hedg ...

  8. unity 解决ScrollRect嵌套滚动问题

    在子级有ScrollRect组件的对象添加以下脚本: using UnityEngine; using System.Collections; using UnityEngine.UI; using ...

  9. springboot 项目 注意事项

    SpringBoot出现下列错误. Your ApplicationContext is unlikely to start due to a @ComponentScan of the defaul ...

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

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