1.函數(shù)聲明和函數(shù)表達式有什么區(qū)別
函數(shù)聲明:function functionName(){}
函數(shù)表達式:var fn = function(){}
函數(shù)聲明會提前,函數(shù)表達式可以省略標識符(函數(shù)名)。
2.什么是變量的聲明前置?什么是函數(shù)的聲明前置
- 變量聲明前置就是在一個作用域塊中,所有的變量都被放在塊的開始處聲明。
- 和變量聲明前置一樣,執(zhí)行代碼之前會先讀取函數(shù)聲明,只要函數(shù)在代碼中進行了聲明,無論它在哪個位置上進行聲明,js引擎都會將它的聲明放在范圍作用域的頂部。
3.arguments 是什么
arguments是一個類數(shù)組對象。代表傳給一個function的參數(shù)列表。
arguments對象是函數(shù)內(nèi)部的本地變量;arguments 已經(jīng)不再是函數(shù)的屬性了。可以在函數(shù)內(nèi)部通過使用 arguments 對象來獲取函數(shù)的所有參數(shù)。這個對象為傳遞給函數(shù)的每個參數(shù)建立一個條目,條目的索引號從0開始。它包括了函所要調(diào)用的參數(shù)。object對象。類數(shù)組。
4.函數(shù)的"重載"怎樣實現(xiàn)
在JS中,沒有重載。同名函數(shù)會覆蓋。但可以在函數(shù)體針對不同的參數(shù)調(diào)用來模擬重載。
function printPeopleInfo(name, age, sex){
if(name){
console.log(name);
}
if(age){
console.log(age);
}
if(sex){
console.log(sex);
}
}
printPeopleInfo('Byron', 26);//Byron 26
printPeopleInfo('Byron', 26, 'male');//Byron 26 male
5.立即執(zhí)行函數(shù)表達式是什么?有什么作用
(function(){
var a = 1;
})()
作用: 隔離作用域,防止變量出錯;
6.求n!,用遞歸來實現(xiàn)
function multiply(n){
if(n===1){
return 1;
}
return n*multiply(n-1);
}
multiply(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: 男
['饑人谷',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);//會報錯,b is not defined
10. 如下代碼的輸出?為什么
sayName('world');//hello world函數(shù)聲明前置
sayAge(10);//報錯,函數(shù)表達式不會前置。
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
}
x變成30
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
},
Scope:bar.[[scope]] // globalContext.AO
}
foo.[[scope]] = barContext.AO
3.調(diào)用foo()
fooContext = {
AO:{},
scope:foo.[[scope]]
}
輸出結(jié)果為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)用立即執(zhí)行函數(shù)
functionContext = {
AO:{},
Scope:function.[[scope]]//barContext.AO
}
結(jié)果為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
fn:function
fn3:function
},
Scope:null
}
fn.[[scope]] = globalContext.AO
fn3.[[scope]] = globalContext.AO
2.調(diào)用fn()
fnContext = {
AO:{
a:undefined
fn2:function
},
Scope:fn.[[scope]] // globalContext.AO
}
fn2.[[scope]] = fnContext.AO
3.fn3Context = {
AO:{
a:200
},
Scope:fn3Context.[scope]]//globalContext.AO
}
fn2ConText = {
AO:{
a:20
},
Scope:fn2ConText.[[scope]]//fnContext.AO
}
開始執(zhí)行
console.log(a)//undefined 打印
var a = 5//fnContext中a變成5
console.log(a)//5
a++//fnContext.AO中a變?yōu)?
調(diào)用fn3()
fn3()中
console.log(a)//globalContext.AO中的a值為1,打印
a = 200//globalContext.AO中的a變?yōu)?00
調(diào)用fn2()
console.log(a);//fnContext.AO中的a值為6,打印
a = 20;//fnContext.AO中的a變?yōu)?0
繼續(xù)執(zhí)行fn()
console.log(a)//fnContext.AO中的a值為20,打印
fn()結(jié)束
console.log(a)//globalContext.AO中a值為200,打印
//輸出的結(jié)果 undefined 5 1 6 20 200
*/