1.函數(shù)聲明和函數(shù)表達(dá)式有什么區(qū)別
函數(shù)聲明:function Name(){}
?函數(shù)表達(dá)式:var fn = function(){}
兩者的區(qū)別在于函數(shù)聲明會前置出刷,定義的Name函數(shù)名會受到j(luò)avascript的變量名提升機(jī)制的影響气笙,函數(shù)表達(dá)式其實(shí)就是創(chuàng)建一個匿名的函數(shù)聲明并賦值給一個變量,可以省略函數(shù)名芹务。
2.什么是變量的聲明前置概作?什么是函數(shù)的聲明前置
在一個作用域下,var聲明的變量和function聲明的函數(shù)會前置
不在函數(shù)作用域內(nèi),var聲明的變量和function聲明的函數(shù)都會前置到全局最頂部
在函數(shù)作用域內(nèi)湾宙,var聲明的變量和function聲明的函數(shù)則會前置到函數(shù)體的最頂部
就是看變量和函數(shù)的作用于在什么地方,那么就前置到什么地方的最頂部。
3.arguments 是什么
arguments是函數(shù)所有參數(shù)的集合(數(shù)組)侠鳄,可以在函數(shù)內(nèi)使用埠啃,既然是類數(shù)組,可以用arguments[0]來表示第一個參數(shù)伟恶。
4. 函數(shù)的"重載"怎樣實(shí)現(xiàn)
js不支持重載碴开,同名函數(shù)會覆蓋,需要對參數(shù)進(jìn)行非空或者類型的判斷來模擬函數(shù)重載(可以用if條件判斷語句來實(shí)現(xiàn))
function printPeopleInfo(name, age, sex){
if(name){
console.log(name);
}
if(age){
console.log(age);
}
if(sex){
console.log(sex);
}
}
printPeopleInfo('Li', 26);//Li 26
printPeopleInfo('Byron', 26, 'male');//Byron 26 male
5.立即執(zhí)行函數(shù)表達(dá)式是什么博秫?有什么作用
類似于下面的寫法是立即執(zhí)行函數(shù)表達(dá)式:
(function(){
//todo
}())
作用:隔離作用域潦牛,讓函數(shù)里面的變量被保護(hù)起來,外部無法訪問挡育。
6. 求n!巴碗,用遞歸來實(shí)現(xiàn)
function fn(n){
if(n===1){
return 1;
}
return n*fn(n-1);
}
fn(3);
fn(3)就返回6
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, '男');
getInfo('小谷', 3);
getInfo('男');
"name:饑人谷"
"age:2"
"sex:男"
[object Arguments] {
0: "饑人谷",
1: 2,
2: "男"
}
"name:valley"
"name:小谷"
"age:3"
"sex:undefined"
[object Arguments] {
0: "小谷",
1: 3
}
"name:valley"
"name:男"
"age:undefined"
"sex:undefined"
[object Arguments] {
0: "男"
}
"name:valley"
8. 寫一個函數(shù)静盅,返回參數(shù)的平方和良价?
function sumOfSquares(){
var be=0;
for(var i=0;i<arguments.length;i++){
be+=arguments[i]*arguments[i];
}
return be;
};
var result = sumOfSquares(2,3,4)
var result2 = sumOfSquares(1,3)
console.log(result) //29
console.log(result2) //10
9. 如下代碼的輸出?為什么
console.log(a);
var a = 1;
console.log(b); ////ReferenceError: b is not defined蒿叠,沒有b變量的聲明
10. 如下代碼的輸出明垢?為什么
sayName('world'); //變量聲明到最前面
sayAge(10); //表達(dá)式形式不會被前置,所以執(zhí)行到這里沒有sayAge這個函數(shù)市咽,報錯典阵。
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};
"hello "
"world"
"error"
"TypeError: sayAge is not a function
11. 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
}
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
}
x變成30
3.調(diào)用foo()
fooContext = {
AO:{},
Scope:foo.[[scope]] = globalContext.AO
}
輸出10
總之就是在當(dāng)前作用域下找重窟,沒有再往上找躏升。
12. 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
globalContext = {
AO:{
x:10,
bar:function,
},
scope:none,
}
bar['scope'] = globalContext.AO;
barContext = {
AO:{
x:30,
foo:function,
},
scope: globalContext.AO,
}
fooContext = {
AO:{},
scope:barContext.AO,
}
輸出30
13. 以下代碼輸出什么? 寫出作用域鏈的查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}
輸出30
globalContext = {
AO:{
x:10
bar:function
},
Scope:null
}
bar.[[scope]] = globalContext.AO
barContext = {
AO:{
x:30
function
},
Scope:bar.[[scope]] =globalContext.AO
}
function.[[scope]] = barContext.AO
functionContext = {
AO:{},
Scope:function.[[scope]]=barContext.AO
}
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)
globalContext = {
AO:{
a:200,
fn:function,
fn3:function,
},
scope:none;
}
fn['scope'] = globalContext.AO;
fn3['scope'] = globalContext.AO;
fn3Context = {
AO:{},
scope:globalContext.AO,
}
fnContext = {
AO:{
a:20,
fn2:function,
},
scope:globalContext.AO
}
fn2['scope'] = fnContext.AO;
fn2Context = {
AO:{},
scope:fnContext.AO
}