1. 函數(shù)聲明和函數(shù)表達(dá)式有什么區(qū)別
函數(shù)聲明:function functionName(){ }
聲明會提前,調(diào)用函數(shù)時可以放在任何地方。
函數(shù)表達(dá)式:var printName = function(){ }
調(diào)用函數(shù)必須放在表達(dá)式后。
2. 什么是變量的聲明前置?什么是函數(shù)的聲明前置
聲明前置:所有的變量的聲明語句,都會被提升到代碼的頭部。
console.log(a);
var a = 1;
//實際運行的是下面的代碼
var a;
console.log(a);
a = 1;
//最后的結(jié)果是顯示undefined,表示變量a已聲明,但還未賦值。
函數(shù)的聲明前置:和變量的聲明前置類似,即使函數(shù)寫在最后也可以在前面語句調(diào)用,前提是函數(shù)聲明部分已經(jīng)被下載到本地。
fn(); // "1"
function fn(){
console.log('1');
}
3. arguments 是什么
arguments對象包含了函數(shù)運行時的所有參數(shù),可用她獲取函數(shù)內(nèi)的所有參數(shù)。arguments[0]就是第一個參數(shù),arguments[1]就是第二個參數(shù),以此類推。這個對象只有在函數(shù)體內(nèi)部,才可以使用。
4. 函數(shù)的"重載"怎樣實現(xiàn)
在JavaScript中沒有函數(shù)重載的概念,函數(shù)通過名字確定唯一性,參數(shù)不同也被認(rèn)為是相同的函數(shù),后面的覆蓋前面的。但可以在函數(shù)體內(nèi)針對參數(shù)的不同來調(diào)用執(zhí)行相應(yīng)的邏輯。
function printPeopleInfo(name, age, sex){
if(name){
console.log(name);
}
if(age){
console.log(age);
}
if(sex){
console.log(sex);
}
}
printPeopleInfo('Byron', 26);
printPeopleInfo('Byron', 26, 'male');
5. 立即執(zhí)行函數(shù)表達(dá)式是什么?有什么作用
有時需要在定義函數(shù)之后,立即調(diào)用該函數(shù),就使用立即調(diào)用的函數(shù)表達(dá)式(IIFE)。但不能在函數(shù)的定義之后加上圓括號,這會產(chǎn)生語法錯誤。解決方法就是不要讓function出現(xiàn)在行首,讓引擎將其理解成一個表達(dá)式。
//最簡單的處理,就是將其放在一個圓括號里面。
(function(){ /* code */ }());
// 或者
(function(){ /* code */ })();
//任何讓解釋器以表達(dá)式來處理函數(shù)定義的方法,都能產(chǎn)生同樣的效果
var i = function(){ return 10; }();
true && function(){ /* code */ }();
0, function(){ /* code */ }();
!function(){ /* code */ }();
~function(){ /* code */ }();
-function(){ /* code */ }();
+function(){ /* code */ }();
通常情況下,只對匿名函數(shù)使用這種“立即執(zhí)行的函數(shù)表達(dá)式”。它的目的有兩個:一是不必為函數(shù)命名,避免了污染全局變量;二是IIFE內(nèi)部形成了一個單獨的作用域,可以封裝一些外部無法讀取的私有變量。
6. 求n!,用遞歸來實現(xiàn)
function factorial(n){
if(n === 1){
return 1;
}
return n*factorial(n-1);
}
factorial(5);
7. 以下代碼輸出什么?
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. 寫一個函數(shù),返回參數(shù)的平方和?
function sumOfSquares(){
var sum = 0;
for(var i = 0; i < arguments.length; i++){
sum = sum + arguments[i]*arguments[i];
}
return sum;
}
var result = sumOfSquares(2,3,4)
var result2 = sumOfSquares(1,3)
console.log(result) //29
console.log(result) //10
9. 如下代碼的輸出?為什么
console.log(a); //undefined
var a = 1;
console.log(b); //報錯
10. 如下代碼的輸出?為什么
sayName('world'); //'hello' 'world'
sayAge(10); //報錯
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()
}
/*
1.
globalContext = {
AO: {
x: 10
foo: function
bar: function
},
Scope: null
}
foo.[[scope]] = globalContext.AO
bar.[[scope]] = globalContext.AO
2.調(diào)用bar
barContext = {
AO: {
x: 30
},
Scope: bar.[[scope]] //globalContext.AO
}
3.調(diào)用foo
fooContext = {
AO: { },
Scope: foo.[[scope]] // globalContext.AO
}
輸出10
*/
12. 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
/*
1.
globalContext = {
AO: {
x: 10
bar: function
},
Scope: null
}
bar.[[scope]] = globalContext.AO
2.調(diào)用bar
barContext = {
AO: {
x: 30
foo: function
},
Scope: bar.[[scope]] // globalContext.AO
}
foo.[[scope]] = barContext.AO
3.調(diào)用foo
fooContext = {
AO: { },
Scope:foo.[[scope]] // barContext.AO
}
輸出30
*/
13. 以下代碼輸出什么? 寫出作用域鏈的查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}
/*
1.
globalContext = {
AO: {
x: 10
bar: function
},
Scope: null
}
bar.[[scope]] = globalContext.AO
2.調(diào)用bar
barContext = {
AO: {
x: 30
function
},
Scope: bar.[[scope]] // globalContext.AO
}
function.[[scope]] = barContext.AO
3.調(diào)用function
functionContext = {
AO: { },
Scope:function.[[scope]] // barContext.AO
}
輸出30
*/
14. 以下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var a = 1;
function fn(){
console.log(a)
var a = 5
console.log(a)
a++
var a
fn3()
fn2()
console.log(a)
function fn2(){
console.log(a)
a = 20
}
}
function fn3(){
console.log(a)
a = 200
}
fn()
console.log(a)
作用域鏈
1.
globalContext = {
AO: {
a: 1 //fn3賦值為200
fn: function
fn3: function
},
Scope: null
}
fn.[[scope]] = globalContext.AO
fn3.[[scope]] = globalContext.AO
2.調(diào)用fn
fnContext = {
AO: {
a: undifined //賦值為5、a++為6、fn2賦值為20
fn2: function
},
Scope: fn.[[scope]] //globalContext.AO
}
fn2.[[scope]] = fnContext.AO
3.調(diào)用fn3
fn3Context = {
AO: {
a: 200
},
Scope: fn3.[[scope]] // globalContext.AO
}
4.調(diào)用fn2
fn2Context = {
AO: {
a: 20
},
Scope: fn2.[[scope]] // fnContext.AO
}
輸出:
undefined
5
1
6
20
200