任務(wù)
- 函數(shù)聲明和函數(shù)表達(dá)式有什么區(qū)別
答:函數(shù)聲明:function functionName(){}
??函數(shù)表達(dá)式:var fn = function(){}
??函數(shù)聲明會(huì)提前,函數(shù)表達(dá)式可以省略標(biāo)識(shí)符(函數(shù)名)。 - 什么是變量的聲明前置?什么是函數(shù)的聲明前置
答:所謂的變量聲明前置就是在一個(gè)作用域塊中,所有的變量都被放在塊的開(kāi)始處聲明。和變量聲明前置一樣,執(zhí)行代碼之前會(huì)先讀取函數(shù)聲明,只要函數(shù)在代碼中進(jìn)行了聲明,無(wú)論它在哪個(gè)位置上進(jìn)行聲明,js引擎都會(huì)將它的聲明放在范圍作用域的頂部。 - arguments 是什么
答:arguments是一個(gè)類(lèi)數(shù)組對(duì)象。代表傳給一個(gè)function的參數(shù)列表。
arguments對(duì)象是函數(shù)內(nèi)部的本地變量;arguments 已經(jīng)不再是函數(shù)的屬性了。可以在函數(shù)內(nèi)部通過(guò)使用 arguments 對(duì)象來(lái)獲取函數(shù)的所有參數(shù)。這個(gè)對(duì)象為傳遞給函數(shù)的每個(gè)參數(shù)建立一個(gè)條目,條目的索引號(hào)從0開(kāi)始。它包括了函所要調(diào)用的參數(shù)。object對(duì)象。類(lèi)數(shù)組。 - 函數(shù)的"重載"怎樣實(shí)現(xiàn)
答:在JS中,沒(méi)有重載。同名函數(shù)會(huì)覆蓋。但可以在函數(shù)體針對(duì)不同的參數(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);//Byron 26
printPeopleInfo('Byron', 26, 'male');//Byron 26 male
- 立即執(zhí)行函數(shù)表達(dá)式是什么?有什么作用
答:
(function(){
var a = 1;
})()
作用: 隔離作用域。
其他寫(xiě)法
(function fn1() {});
// 在數(shù)組初始化器內(nèi)只能是表達(dá)式
[function fn2() {}];
// 逗號(hào)也只能操作表達(dá)式
1, function fn3() {};
- 求n!,用遞歸來(lái)實(shí)現(xiàn)
答:
遞歸:
自己調(diào)用自己
設(shè)定終止條件
優(yōu)點(diǎn): 算法簡(jiǎn)單
缺點(diǎn): 效率低
求n!
function factor(n){
if(n === 1) {
return 1
}
return n * factor(n-1) } factor(5)
- 以下代碼輸出什么?
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: 男
name valley*/
getInfo('小谷', 3);
/*name: 小谷
age: 3
sex: undefined
name valley*/
getInfo('男');
/* name: 男
age: undefined
sex: undefined
name valley*/
- 寫(xiě)一個(gè)函數(shù),返回參數(shù)的平方和?
function sumOfSquares(){
var sum=0;
for(var i=0;i<arguments.length;i++){
sum=sum+arguments[i]*arguments[i];
}
console.log(sum);
}
var result = sumOfSquares(2,3,4)
var result2 = sumOfSquares(1,3)
console.log(result) //29
console.log(result2) //10
- 如下代碼的輸出?為什么
console.log(a);
var a = 1;
console.log(b);//b is not defined
- 如下代碼的輸出?為什么
sayName('world');//hello world,函數(shù)聲明前置
sayAge(10);//sayAge is not a function,函數(shù)表達(dá)式不前置
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};
- 如下代碼輸出什么? 寫(xiě)出作用域鏈查找過(guò)程偽代碼
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 */
- 如下代碼輸出什么? 寫(xiě)出作用域鏈查找過(guò)程偽代碼
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 */
- 以下代碼輸出什么? 寫(xiě)出作用域鏈的查找過(guò)程偽代碼
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*/
- 以下代碼輸出什么? 寫(xiě)出作用域鏈查找過(guò)程偽代碼
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 }
開(kāi)始執(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
*/