建网站专业,规划院网站建设书,wordpress 改成论坛,国外化妆品网站模板规则#xff1a;
函数的提升优先于变量提升。同名的函数会覆盖同名的函数与变量。同名的变量不会覆盖同名的函数。
示例代码1#xff1a; !DOCTYPE htmlhtml langzhheadmeta charsetUTF-8 /meta namevi…规则
函数的提升优先于变量提升。同名的函数会覆盖同名的函数与变量。同名的变量不会覆盖同名的函数。
示例代码1 !DOCTYPE html
html langzh
head
meta charsetUTF-8 /
meta nameviewport contentwidthdevice-width, initial-scale1.0 /
meta http-equivX-UA-Compatible contentieedge /
title变量提升与函数提升/title
/head
body
script typetext/javascript
var a 20;
function fn(){
console.log(fn)
}
function fn(){
console.log(covert fn)
}
function a(){
console.log(cover a)
}
console.log(a);
fn();
var fn I want to cover function name fn.
console.log(fn)
函数的提升优于变量提升。同名的函数会覆盖函数与变量。
/script
/body
/html 控制台输出 解释 !DOCTYPE html
html langzh
head
meta charsetUTF-8 /
meta nameviewport contentwidthdevice-width, initial-scale1.0 /
meta http-equivX-UA-Compatible contentieedge /
title变量提升与函数提升/title
/head
body
script typetext/javascript
//真正的执行顺序为
//函数提升
function fn() {
console.log(fn)
}
//函数提升
function fn() {
console.log(covert fn)
}
//函数提升
function a() {
console.log(cover a)
}
//变量提升
var a undefined;
//变量提升
var fn undefined;
a 20;
console.log(a); //20
fn(); //covert fn
fn I want to cover function name fn.;
console.log(fn); //I want to cover function name fn.
/script
/body
/html 示例代码2 !DOCTYPE html
html langzh
head
meta charsetUTF-8 /
meta nameviewport contentwidthdevice-width, initial-scale1.0 /
meta http-equivX-UA-Compatible contentieedge /
title变量提升与函数提升/title
/head
body
script typetext/javascript
function test() {
console.log(foo)
console.log(bar)
var foo hello
console.log(foo)
var bar function() {
return world
}
function foo() {
return hello
}
}
test();
/script
/body
/html 控制台输出 解释 !DOCTYPE html
html langzh
head
meta charsetUTF-8 /
meta nameviewport contentwidthdevice-width, initial-scale1.0 /
meta http-equivX-UA-Compatible contentieedge /
title变量提升与函数提升/title
/head
body
script typetext/javascript
//执行顺序为
function test() {
//函数提升
function foo() {
return hello
}
//变量提升 但是不会覆盖同名的函数
// var foo undefined;
//变量提升
var bar undefined;
console.log(foo)
console.log(bar)
foo hello
console.log(foo)
bar function() {
return world
}
}
test();
/script
/body
/html 更多专业前端知识请上
【猿2048】www.mk2048.com