1
var a=12;
function fn(){
console.log(a) //undefined
var a=45;
console.log(a) //45
}
fn()
2
var a=12;
function fn(){
console.log(a) //12
a=45;
console.log(a) //45
}
fn()
3
function fn(){
console.log(11)
function ff(){
console.log(22)
}
ff()
}
fn() //11
ff() //ff is not defined
函数归属谁,跟它在哪调用无关,而是在哪定义有关
4
function fn(){
console.log(5)
fn()
}
fn() //自调用5
5
var a=12;
function fn(){
console.log(a) //undefined
return 4;
var a=45;
}
fn()
函数内部的return,不影响函数内部的预解释
6
var a=45;
function fn(a){
console.log(a) //undefined
}
fn()
a是私有变量
7
var a=45;
function fn(a){
console.log(a) //5
}
fn(5)
8
var a=123;
function fun(){
console.log(a) //123
}
fun()
9
var a=123;
function fun(){
console.log(a) //undefined
var a=456;
}
fun()
console.log(a) //123
10
var a=123;
function fun(){
console.log(a) //123
a=456;
}
fun()
console.log(a) //456
fun存储模块没有var,所有找上一级,所以,a=456,改变了a的值
11
var a=123;
function fun(a){
console.log(a) //undefined
a=456
}
fun()
console.log(a) //123
fun(),里面有形参a
12
var a=123;
function fun(a){
console.log(a) //123
a=456
}
fun(123)
console.log(a) //123
形参a,实参123,
13
var a=12;
function fn(){
console.log(a) //undefined
var a=45;
console.log(a) //45
}
fn()
14
var a=12;
function fn(){
console.log(a) //12
a=45;
console.log(a) //45
}
fn()
15
function fn(){
console.log(11)
function ff(){
console.log(22)
}
ff()
}
fn() //11
ff() //ff is not defined
16
var a=45;
function fn(a){
console.log(a) //undefined
}
fn()
17
函数和函数变量名重名
function fn(a){
console.log(a) //function a(){}
var a=123;
console.log(a) //123
function a(){}
console.log(a) //123
var b=function(){}
console.log(b) //function(){}
function d(){}
}
fn(1)
如果我们声明的变量和函数名同名了,在预解释的时候,只声明一次
18
function test(a,b){
console.log(a) //1
c=0;
var c;
a=3;
b=2;
console.log(b) //2
function b(){}
function d(){}
console.log(b) //2
}
test(1)
19
function test(a,b){
console.log(a) //function a(){}
console.log(b) //undefined
var b=234;
console.log(b) //234
a=123;
console.log(a) //123
function a(){}
var a;
b=234;
var b=function(){}
console.log(a) //123
console.log(b) //function(){}
}
test(1)