1. 函數(shù)聲明和函數(shù)表達式有什么區(qū)別
函數(shù)聲明:function functionName(){ }
聲明會提前,調用函數(shù)時可以放在任何地方第股。
函數(shù)表達式:var printName = function(){ }
調用函數(shù)必須放在表達式后赦邻。
2. 什么是變量的聲明前置骂远?什么是函數(shù)的聲明前置
聲明前置:所有的變量的聲明語句衡未,都會被提升到代碼的頭部。
console.log(a);
var a = 1;
//實際運行的是下面的代碼
var a;
console.log(a);
a = 1;
//最后的結果是顯示undefined峰伙,表示變量a已聲明,但還未賦值该默。
函數(shù)的聲明前置:和變量的聲明前置類似瞳氓,即使函數(shù)寫在最后也可以在前面語句調用,前提是函數(shù)聲明部分已經被下載到本地栓袖。
fn(); // "1"
function fn(){
console.log('1');
}
3. arguments 是什么
arguments對象包含了函數(shù)運行時的所有參數(shù)匣摘,可用她獲取函數(shù)內的所有參數(shù)店诗。arguments[0]就是第一個參數(shù),arguments[1]就是第二個參數(shù)音榜,以此類推庞瘸。這個對象只有在函數(shù)體內部,才可以使用赠叼。
4. 函數(shù)的"重載"怎樣實現(xiàn)
在JavaScript中沒有函數(shù)重載的概念擦囊,函數(shù)通過名字確定唯一性,參數(shù)不同也被認為是相同的函數(shù)嘴办,后面的覆蓋前面的瞬场。但可以在函數(shù)體內針對參數(shù)的不同來調用執(zhí)行相應的邏輯。
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ù)表達式是什么涧郊?有什么作用
有時需要在定義函數(shù)之后贯被,立即調用該函數(shù),就使用立即調用的函數(shù)表達式(IIFE)妆艘。但不能在函數(shù)的定義之后加上圓括號刃榨,這會產生語法錯誤。解決方法就是不要讓function出現(xiàn)在行首双仍,讓引擎將其理解成一個表達式枢希。
//最簡單的處理,就是將其放在一個圓括號里面朱沃。
(function(){ /* code */ }());
// 或者
(function(){ /* code */ })();
//任何讓解釋器以表達式來處理函數(shù)定義的方法苞轿,都能產生同樣的效果
var i = function(){ return 10; }();
true && function(){ /* code */ }();
0, function(){ /* code */ }();
!function(){ /* code */ }();
~function(){ /* code */ }();
-function(){ /* code */ }();
+function(){ /* code */ }();
通常情況下,只對匿名函數(shù)使用這種“立即執(zhí)行的函數(shù)表達式”逗物。它的目的有兩個:一是不必為函數(shù)命名搬卒,避免了污染全局變量;二是IIFE內部形成了一個單獨的作用域翎卓,可以封裝一些外部無法讀取的私有變量契邀。
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.調用bar
barContext = {
AO: {
x: 30
},
Scope: bar.[[scope]] //globalContext.AO
}
3.調用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.調用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
}
輸出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.調用bar
barContext = {
AO: {
x: 30
function
},
Scope: bar.[[scope]] // globalContext.AO
}
function.[[scope]] = barContext.AO
3.調用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.調用fn
fnContext = {
AO: {
a: undifined //賦值為5矩肩、a++為6现恼、fn2賦值為20
fn2: function
},
Scope: fn.[[scope]] //globalContext.AO
}
fn2.[[scope]] = fnContext.AO
3.調用fn3
fn3Context = {
AO: {
a: 200
},
Scope: fn3.[[scope]] // globalContext.AO
}
4.調用fn2
fn2Context = {
AO: {
a: 20
},
Scope: fn2.[[scope]] // fnContext.AO
}
輸出:
undefined
5
1
6
20
200