JavaScript中的函數運行在它們被定義的作用域里,而不是它們被執行的作用域里。
-
函數聲明和函數表達式有什么區別
函數聲明和函數表達式非常相似,有著相同的語法。函數聲明的函數名不能省略。函數表達式的函數名,可以省略。當省略函數名的時候,該函數就成為了匿名函數。
函數聲明會被整體提升,調用可以放在聲明的前面。
function a(){}
而函數表達式是一個表達式,常出現在賦值的式子中。只有聲明變量的部分會被提升,調用要放在聲明的后面
function (){}
-
什么是變量的聲明前置?什么是函數的聲明前置
變量的聲明前置:所有變量的聲明語句會提升到代碼的頭部
函數的聲明前置:采用function命令聲明函數時,整個函數會像變量聲明一樣,被提升到代碼頭部 -
arguments 是什么
arguments對象在函數內部用來獲取到該函數的所有傳入的參數.arguments對象不是一個數組 。它類似于數組,但除了length之外沒有任何數組屬性 -
函數的"重載"怎樣實現
JS是不支持重載的,第二個同名函數會覆蓋前一個。但是可以通過arguments對象來實現“重載”。
實現重載常用的方式:
1、根據傳入參數的類型執行不同的操作。
2、利用參數中特殊的參數值進行不同的操作。
3、根據參數的個數進行重載。 -
立即執行函數表達式是什么?有什么作用
立即執行函數表達式常見兩種形式
(function (){console.log(123)}())
(function (){console.log(123)})()
立即執行函數可以創建一個獨立的作用域,防止變量污染。 - 求n!,用遞歸來實現
function a(n){
if(n>1){
return n * a(n-1)
}
if(n===1){
return 1
}
}
代碼題
function getInfo(name, age, sex){ console.log('name:',name); console.log('age:', age); console.log('sex:', sex); console.log(arguments); arguments[0] = 'valley'; console.log('name', name); }
getInfo('饑人谷', 2, '男');// name:饑人谷 age:2 sex:男 ['饑人谷', 2, '男'] name valley
getInfo('小谷', 3); //name: 小谷 age:3 sex:undefined ["小谷", 3] name valley
getInfo('男'); //name:男 age: undefined sex: undefined ["男"] name valley
8. 寫一個函數,返回參數的平方和?
function sumOfSquares(){
var sum = 0
for(i=0;i<arguments.length;i++){
sum += Math.pow(arguments[i],2)
}
return sum
}
var result = sumOfSquares(2,3,4)
var result2 = sumOfSquares(1,3)
console.log(result) //29
console.log(result2) //10
9. 如下代碼的輸出?為什么
console.log(a); //undefined 變量提升
var a = 1;
console.log(b); // b is not defined
10. 如下代碼的輸出?為什么
sayName('world'); //hello world 函數聲明提升
sayAge(10); //sayAge is not a function 變量提升 函數表達式不提升
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};
11. 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
}
輸出10
下面是偽代碼
globalContext = {
AO: {
x: 10
foo: function
bar: function
}
Scope: null
}
foo.[[scope]] = globalContext.AO
bar.[[scope]] = globalContext.AO
2.調用bar()
barContext = {
AO: {
x: 30
}
Scope: bar.[[scope]] = globalContext.AO
}
3.調用foo()
fooContext = {
AO: {}
Scope: foo.[[scope]] = globalContext.AO
12. 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
輸出30
下面是偽代碼
globalContext = {
AO: {
x: 10
bar: function
}
Scope: null
}
bar.[[scope]] = globalContext.AO
2.調用bar()
barContext = {
AO: {
x: 30
foo: function
}
Scope: bar.[[scope]] = globalContext.AO
}
foo.[[scope]] = barContext.AO
3.調用foo()
fooContext = {
AO: {}
Scope: foo.[[scope]] = barContext.AO
}
13. 以下代碼輸出什么? 寫出作用域鏈的查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}
輸出 30
下面是偽代碼
1 .
globalContext = {
AO: {
x: 10
bar: function
}
Scope: null
}
bar.[[scope]] = globalContext.AO
2.調用bar()
barContext = {
AO: {
x: 30
function(){}
}
Scope: bar.[[scope]] = globalContext.AO
}
function.[[scope]] = barContext.AO
3.調用function()
functionContext = {
AO: {}
Scope: function.[[scope]] = barContext.AO
}
14. 以下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var a = 1;
function fn(){
console.log(a) //重寫過程 var a
var a = 5 //console.log(a) a = undefined
console.log(a) //a=5
a++ //console.log(a) a = 5
var a //a++
fn3() // a = 1
fn2() // a = 6
console.log(a)// a = 20
function fn2(){
console.log(a) // 6
a = 20
}
}
function fn3(){
console.log(a) // 1
a = 200
}
fn()
console.log(a) // a = 200
下面是偽代碼
globalContext = {
AO: {
a = 1
fn : function
fn3 : function
}
Scope : null
}
fn.[[Scope]] = globalContext.AO
fn3.[[Scope]] = globalContext.AO
2.調用fn
fnContext = {
AO:{
a = undefined 5 6 20
fn2 = function
}
Scope : fn.[[Scope]] = globalConstext.AO
}
fn2.[[Scope]] = fnContext.AO
3.調用fn3
fn3Context = {
AO:{
}
Scope : fn3.[[Scope]] = globalContext.AO
}
4.調用fn2
fn2Context = {
AO: {
}
Scope: fn2.[[scope]] = fnContext.AO
}
5.console.log(a) globalContext.AO