任務(wù)
- 函數(shù)聲明和函數(shù)表達式有什么區(qū)別
答:函數(shù)聲明:function functionName(){}
??函數(shù)表達式:var fn = function(){}
??函數(shù)聲明會提前云茸,函數(shù)表達式可以省略標(biāo)識符(函數(shù)名)梳侨。 - 什么是變量的聲明前置蓖捶?什么是函數(shù)的聲明前置
答:所謂的變量聲明前置就是在一個作用域塊中,所有的變量都被放在塊的開始處聲明辐啄。和變量聲明前置一樣,執(zhí)行代碼之前會先讀取函數(shù)聲明烦租,只要函數(shù)在代碼中進行了聲明豺鼻,無論它在哪個位置上進行聲明,js引擎都會將它的聲明放在范圍作用域的頂部挨约。 - 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ù)組拷获。 - 函數(shù)的"重載"怎樣實現(xiàn)
答:在JS中,沒有重載开镣。同名函數(shù)會覆蓋刀诬。但可以在函數(shù)體針對不同的參數(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ù)表達式是什么邪财?有什么作用
答:
(function(){
var a = 1;
})()
作用: 隔離作用域陕壹。
其他寫法
(function fn1() {});
// 在數(shù)組初始化器內(nèi)只能是表達式
[function fn2() {}];
// 逗號也只能操作表達式
1, function fn3() {};
- 求n!,用遞歸來實現(xiàn)
答:
遞歸:
自己調(diào)用自己
設(shè)定終止條件
優(yōu)點: 算法簡單
缺點: 效率低
求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*/
- 寫一個函數(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ù)表達式不前置
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};
- 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
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 */
- 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
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 */
- 以下代碼輸出什么? 寫出作用域鏈的查找過程偽代碼
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*/
- 以下代碼輸出什么毕匀? 寫出作用域鏈查找過程偽代碼
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
*/