首页 技术 正文
技术 2022年11月8日
0 收藏 912 点赞 1,636 浏览 1604 个字

网上找了两个经典的例子

var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
alert(foo);
}
bar(); // 10
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);// 1

在JavaScript中,函数、变量的声明都会被提升(hoisting)到该函数或变量所在的scope的顶部。即——JavaScript的变量提升。

javascript中一个名字(name)以四种方式进入作用域(scope),其优先级顺序如下:

  • 语言内置:所有的作用域中都有 this 和 arguments 关键字;
  • 形式参数:函数的参数在函数作用域中都是有效的;
  • 函数声明:形如function foo() {};
  • 变量声明:形如var bar;

名字声明的优先级如上所示。也就是说如果一个变量的名字与函数的名字相同,那么函数的名字会覆盖变量的名字,无论其在代码中的顺序如何。但名字的初始化却是按其在代码中书写的顺序进行的,不受以上优先级的影响。

(function(){
var foo;
console.log(typeof foo); //function function foo(){} foo = "foo";
console.log(typeof foo); //string var foo;
})();
function test() {
foo(); // TypeError "foo is not a function"
bar(); // "this will run!"
var foo = function () { // function expression assigned to local variable 'foo'
alert("this won't run!");
}
function bar() { // function declaration, given the name 'bar'
alert("this will run!");
}
}
test();

变量的提升(Hoisting)只是其定义提升,而变量的赋值并不会提升

再来看一个例子,下面两段代码其实是等价的:

function foo() {
if (false) {
var x = 1;
}
return;
var y = 1;
}
function foo() {
var x, y;
if (false) {
x = 1;
}
return;
y = 1;
}

创建一个函数的方法有两种:

一种是通过函数声明function foo(){}

另一种是通过定义一个变量var foo = function(){}

function test() {
foo(); // TypeError "foo is not a function"
bar(); // "this will run!"
var foo = function () { // function expression assigned to local variable 'foo'
alert("this won't run!");
}
function bar() { // function declaration, given the name 'bar'
alert("this will run!");
}
}
test();

var foo首先会上升到函数体顶部,然而此时的fooundefined,所以执行报错。而对于函数bar, 函数本身也是一种变量,所以也存在变量上升的现象,但是它是上升了整个函数,所以bar()才能够顺利执行。

本文参考自:

http://www.cnblogs.com/damonlan/archive/2012/07/01/2553425.html

https://segmentfault.com/a/1190000003114255

https://segmentfault.com/a/1190000005794611


作者博客:pspgbhu

作者GitHub:https://github.com/pspgbhu

欢迎转载,但请注明出处,谢谢!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,966
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783