JS函数

226 阅读1分钟

函数预解析

console.log(a); test()因为变量a没有声名过,所以会出现报错。所以需要进行调用,提前定义变量、函数。

console.log(a)
function test(){
    console.log("this is test function")
}
test()
  • 声明:var num; 告诉浏览器在全局作用域中有一个num变量了,如果一个变量只是声明了,但是没有赋值,默认值是undefined。
console.log(a)
function test(){
    console.log("this is test function")
}
test()

console.log(a)
var a = 10;
test()
function test(){
    console.log("this is test function")
}

  预解析的过程,就是查找代码中的var和function这两个关键字,找到以后,将变量和函数提前存到内存中,并给他们赋一个初始值,变量的初始值为undefined,函数的初始值为代码段。

  解读代码的时候,会略过变量和函数的定义,因为变量和函数的定义已经提前放在内存中了,提前储存的变量和函数的值会随着代码的解读而发生变化,也就是变量的赋值和函数的调用。