1.函數(shù)聲明和函數(shù)表達(dá)式有什么區(qū)別
函數(shù)聲明可以看作是函數(shù)的初始化侄刽,我們將給函數(shù)傳參并建立函數(shù)體的表達(dá)式指黎,當(dāng)我門建立完成后,就可以進(jìn)行函數(shù)的表達(dá)式了州丹,做法如下:function foo(){}, foo();
函數(shù)表達(dá)式其實(shí)就是創(chuàng)建一個(gè)匿名的函數(shù)聲明并賦值給一個(gè)變量,如var foo = function () {}董济。
區(qū)別是函數(shù)聲明會(huì)提升装畅,可以在任意位置調(diào)用,而函數(shù)表達(dá)式賦值給變量后,變量提升但此時(shí)是undefined,不能在函數(shù)表達(dá)式前調(diào)用函數(shù)第步。
2.什么是變量的聲明前置均驶?什么是函數(shù)的聲明前置曹质?
變量提升:當(dāng)一個(gè)變量被定義時(shí)有梆,在代碼執(zhí)行前會(huì)先將變量進(jìn)行初始化再執(zhí)行語句。
函數(shù)的聲明前置:當(dāng)一個(gè)函數(shù)以函數(shù)聲明的方式聲明時(shí)醉箕,代碼執(zhí)行前會(huì)首先生成該函數(shù)钾腺,然后再執(zhí)行語句
3.arguments 是什么
argument是類數(shù)組對象,每個(gè)函數(shù)中都存在argument對象讥裤,argument并不是一個(gè)真正的數(shù)組
放棒,所以不具備除length屬性之外的屬性,這個(gè)對象維護(hù)這所有傳入該函數(shù)的參數(shù)列表己英。
通過以下語句可將arguments轉(zhuǎn)化為數(shù)組對象
var args=Array.prototype.slice.call(arguments)
4.函數(shù)的"重載"怎樣實(shí)現(xiàn)
通過判斷參數(shù)是否存在來實(shí)現(xiàn)
function fn(name,age,sex) {
if (name) {
console.log(name);
}
if (age) {
console.log(age);
}
if (sex){
console.log(sex);
}
}
fn('gaojin');
fn('gaojin',24);
fn('gaojin',24,'male');
通過判斷參數(shù)列表的個(gè)數(shù)來實(shí)現(xiàn)间螟,即判斷arguments.length來實(shí)現(xiàn)
function fn(name,age,sex){
if (arguments.length===1) {
console.log(name);
}
if (arguments.length===2) {
console.log(age);
}
if (arguments.length===3) {
console.log(sex);
}
}
5.立即執(zhí)行函數(shù)表達(dá)式是什么?有什么作用
立即執(zhí)行函數(shù)能夠立即執(zhí)行,這樣可以做到隔離作用域厢破,避免變量污染全局荣瑟。
function(){ /* code */ }();// SyntaxError: Unexpected token
一般情況下,也許有人認(rèn)為立即執(zhí)行函數(shù)可能會(huì)是這個(gè)樣子的摩泪,但其實(shí)為了避免解析上的歧義笆焰,JavaScript引擎規(guī)定,如果function關(guān)鍵字出現(xiàn)在行首见坑,一律解釋成語句嚷掠。因此,JavaScript引擎看到行首是function關(guān)鍵字之后荞驴,認(rèn)為這一段都是函數(shù)的定義不皆,不應(yīng)該以圓括號(hào)結(jié)尾,所以就報(bào)錯(cuò)了熊楼。
所以一般情況下要寫成
(function(){ /* code */ }());//常用
或者
(function(){ /* code */ })();
上面兩種寫法都是以圓括號(hào)開頭霹娄,引擎就會(huì)認(rèn)為后面跟的是一個(gè)表示式,而不是函數(shù)定義語句孙蒙,所以就避免了錯(cuò)誤项棠。這就叫做“立即調(diào)用的函數(shù)表達(dá)式”(Immediately-Invoked Function Expression),簡稱IIFE挎峦。
注意,上面兩種寫法最后的分號(hào)都是必須的合瓢。如果省略分號(hào)坦胶,遇到連著兩個(gè)IIFE,可能就會(huì)報(bào)錯(cuò)晴楔。
推而廣之顿苇,任何讓解釋器以表達(dá)式來處理函數(shù)定義的方法,都能產(chǎn)生同樣的效果税弃,比如下面三種寫法纪岁。
var i = function(){ return 10; }();
true && function(){ /* code */ }();
0, function(){ /* code */ }();
6.求n!,用遞歸來實(shí)現(xiàn)
function factorial(i){
if (i===1) {
return 1;
}
else
return i*factorial(i-1);
}
console.log(factorial(5));
輸出120
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("男");
getInfo("饑人谷",2, "男");
輸出 name:饑人谷 age:2 sex:男 ["饑人谷"幔翰,2,"男"] name valley
getInfo("小谷",3);
輸出 name:小谷 age:3 age:undefined ["小谷",3] name valley
getInfo("男");
輸出 name:男 age:undefined sex:undefined [“男”] name valley
8.寫一個(gè)函數(shù)西壮,返回參數(shù)的平方和遗增?
function sumOfSquares(){
var total=0;
if (arguments.length!==0){
for(var i=0;i<arguments.length;i++){
total+=Math.pow(arguments[i],2);
}
return total;
}
else return "輸入有誤";
}
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);
輸出undefined ReferenceError: b is not defined
10. 如下代碼的輸出款青?為什么
sayName('world');
sayAge(10);
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};
hello world
TypeError: sayAge is not a function
原因:在代碼開始解析前會(huì)進(jìn)行變量提升和函數(shù)聲明提升做修,而用函數(shù)聲明方式的函數(shù)在哪里都可以被執(zhí)行,所以返回hello world,而以函數(shù)表達(dá)式方式的饰及,只有變量sayAge提升蔗坯,函數(shù)表達(dá)式還留在原地,在此之前調(diào)用函數(shù)都會(huì)報(bào)錯(cuò)燎含。
- 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
}
輸出結(jié)果 10
globalContent={
AO:{
x:10
foo:function
bar:function
},
scope:globalContent.AO
}
barContent={
AO:{
x:30
},
scope:bar.[[scope]]=globalContent.AO
}
fooContent={
AO:{}
scope:foo.[[scope]]=globalContent.AO
}
- 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
輸出結(jié)果 30,查找過程
globalContext={
AO:{
x:10
bar:function
},
scope:globalContext.AO
}
barContext={
AO{
x:30
foo:function
}宾濒,
scope:bar.[[scope]]=globalcontext.AO
}
fooContext={
AO{},
scope:foo.[[scope]]=barcontext.AO
}
13. 以下代碼輸出什么? 寫出作用域鏈的查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}
輸出結(jié)果 30
globalContext={
AO:{
x:10
bar:function
},
scope:globalContext.AO
}
barContext={
AO:{
x:30
function
},
scope:bar.[[scope]]=globalContext.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)
undefined,5,1,6,20,200
globalContext={
AO:{
a:1/200
fn:function
fn3:function
},
scope:globalContext.AO
}
fnContext={
AO:{
a:undefined/5/6/20
fn2:function
},
scope:bar.[[scope]]=globalContext.AO
}
fn3Context={
AO:{}
scope:fn3.[[scope]]=globalContext.AO
}
}
fn2Context={
AO:{}
scope:fn2.[[scope]]=fnContext.AO
}