1.函數(shù)聲明和函數(shù)表達(dá)式有什么區(qū)別
構(gòu)造函數(shù)常用的兩種方法:函數(shù)聲明和函數(shù)表達(dá)式
//函數(shù)聲明
function sum(a, b) {
return a + b;
}
alert(sum(1, 2));
//函數(shù)表達(dá)式 var s = function sum(a, b) { return a + b; } alert(s(1, 2)); /*///////////////////////////////*/ var s = function(a, b) { return a + b; } alert(s(1, 2)); //以上兩種都可以
這二者區(qū)別是:
可以看到函數(shù)聲明必須有函數(shù)名,而函數(shù)表達(dá)式中的函數(shù)名可以忽略
由于JavaScript解析器對(duì)這兩種定義方式讀取的順序不同所以用函數(shù)聲明定義的函數(shù),函數(shù)可以在函數(shù)聲明之前調(diào)用,而用函數(shù)表達(dá)式定義的函數(shù)只能在聲明之后調(diào)用。
2.什么是變量的聲明前置?什么是函數(shù)的聲明前置
變量聲明前置是將變量的聲明提到當(dāng)前作用域的開頭,比如:
console.log(a); //undefined
var a = 1;
console.log(a); // 1
// 上面的代碼在函數(shù)解析的過程是這樣的
var a;
console.log(a); // undefined
a = 1 ;
console.log(a); // 1
變量聲明前置只是將其提前聲明,對(duì)于它的賦值則還在原來的位置。
函數(shù)的聲明前置,舉個(gè)栗子:
fun1(); // 123
function fun1(){
console.log(123);
}
函數(shù)聲明前置是將函數(shù)聲明提前到當(dāng)前作用域開頭,注意函數(shù)聲明前置比變量聲明前置優(yōu)先級(jí)更高,可以在當(dāng)前作用域下任何地方調(diào)配此函數(shù),區(qū)別于函數(shù)表達(dá)式所必需遵守的先后順序。
3.arguments 是什么
arguments是一個(gè)類似數(shù)組的對(duì)象, 對(duì)應(yīng)于傳遞給函數(shù)的參數(shù)。
arguments 對(duì)象僅在函數(shù)內(nèi)部有效,在函數(shù)外部調(diào)用 arguments 對(duì)象會(huì)出現(xiàn)一個(gè)錯(cuò)誤。
可以使用arguments對(duì)象在函數(shù)中引用函數(shù)的參數(shù)。此對(duì)象包含傳遞給函數(shù)的每個(gè)參數(shù)的條目,比如:
function Resume(name,age,sex){
console.log(arguments[0]); // 等效于console.log(name);
console.log(arguments[1]); // console.log(age);
console.log(arguments[2]); // console.log(sex);
}
Resume('XiaoMing',21,'male');
4.函數(shù)的"重載"怎樣實(shí)現(xiàn)
函數(shù)重載是指:形參不同的多個(gè)同名函數(shù)根據(jù)處理數(shù)據(jù)的不同而返回不同的結(jié)果。
函數(shù)重載主要運(yùn)用于c++,java等強(qiáng)類型語言中,因?yàn)镴avaScript是若類型語言,構(gòu)建同名函數(shù)會(huì)把之前的覆蓋掉,因此在JS中沒用重載,但是可以運(yùn)用一些技巧達(dá)到重載的效果。比如:
function fun1(obj) { alert(1) }
function fun1(obj, obj1, obj2) { alert(3) }
function fun1(obj2,obj3) {alert(2) }
fun1();
這樣的代碼在JS中只會(huì)彈出“2”,因?yàn)楹竺娴暮瘮?shù)會(huì)覆蓋掉前面的同名函數(shù),那要怎么樣才能打到想要的效果呢,這就需要添加判定條件了
function fun1(obj) { alert(1) }
function fun3(obj, obj1, obj2) { alert(3) }
function fun2(obj, obj1) { alert(2) }
function funAll(obj, obj1, obj2, obj3) {
if ( arguments.length == 1) {
fun1(obj);
}
else if ( arguments.length == 2) {
fun2(obj, obj1);
}
else if ( arguments.length == 3) {
fun3(obj, obj1, obj2);
}
}
funAll("");
funAll("", "");
funAll("", "","");
5.立即執(zhí)行函數(shù)表達(dá)式是什么?有什么作用
立即執(zhí)行函數(shù)模式是一種語法,可以讓你的函數(shù)在定義后立即被執(zhí)行,作用是隔離作用域,獨(dú)立起來不影響全局
(function () {
alert('hello world');
})()
6.求n!,用遞歸來實(shí)現(xiàn)
function factor(n){
if(n < 0){
return false;
}else if(n <= 1){
return 1;
}else{
return n * factor(n-1);
}
}
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, '男'); //name:饑人谷 age:2 sex:男 ['饑人谷', 2, '男'] name valley
getInfo('小谷', 3); //name:小谷 age:3 sex:undefined ['饑人谷', 2, '男'] name valley
getInfo('男'); //name:男 age:undefined sex:undefined ['饑人谷', 2, '男'] name valley
8.寫一個(gè)函數(shù),返回參數(shù)的平方和?
function sumOfSquares(a,b,c){
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
sum += (arguments[i]) * (arguments[i]);
}
return sum;
}
var result = sumOfSquares(2,3,4)
var result2 = sumOfSquares(1,3)
console.log(result) //29
console.log(result2) //10
9.如下代碼的輸出?為什么
console.log(a); //undefined 變量a聲明前置,但是還未賦值
var a = 1;
console.log(b); //報(bào)錯(cuò),因?yàn)闆]有聲明變量b
10. 如下代碼的輸出?為什么
sayName('world'); // hello world 函數(shù)聲明前置,可以隨意調(diào)用
sayAge(10); // 報(bào)錯(cuò),因?yàn)榇撕瘮?shù)必須必須在其函數(shù)表達(dá)式之后調(diào)用
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};
11.如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10
bar() //10
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.barContext = {
AO: {
x: 30
}
Scope: bar.[[scope]] = globalContext.AO;
}
3.fooContext = {
AO: {}
Scope: foo.[[scope]] = globalContext.AO;
} */
12.如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10;
bar() // 30
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
//////////////////////////////////////////////////
/* 1.globalContext = {
AO:{
X: 10
bar: function(){}
}
scopr: null
}
bar.[[scope]] = globalContext.AO
2.barContext = {
AO: {
x: 30
foo: function (){}
}
Scope: bar.[[scope]] = globalContext.AO
}
foo.[[scope]] = barContext.AO
3.fooContext = {
AO: {}
Scope: foo.[[scope]] = barContext.AO
} */
13.以下代碼輸出什么? 寫出作用域鏈的查找過程偽代碼
var x = 10;
bar() //30
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}
/////////////////////////////////////////////////////////
/* 1.globalContext = {
AO: {
x: 10
bar: function (){}
}
Scope: null
}
bar.[[scope]] = globalContext.AO
2.barContext = {
AO: {
x: 30
IIFE
}
Scope: bar.[[scope]] = globalContext.AO
}
IIFE.[[scope]] = barContext.AO
3.IIFEContext {
AO: {}
Scope: IIFE.[[scope]] = barContext.AO
}*/
14.以下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var a = 1;
function fn(){
console.log(a) //undefined
var a = 5
console.log(a) // 5
a++
var a
fn3() // 1
fn2() // 6
console.log(a) //20
function fn2(){
console.log(a)
a = 20
}
}
function fn3(){
console.log(a)
a = 200
}
fn()
console.log(a) //200
//////////////////////////////////////////////////////
/*1.globalContext = {
AO: {
a: 1
fn: function(){}
fn3: function(){}
}
Scope: null
}
fn.[[scope]] = globalContext.AO
fn3.[[scope]] = globalContext.AO
2.fnContext = {
AO: {
a: 5
fn2: function(){}
}
Scope: fn.[[scope]] = globalContext.AO
}
fn2.[[scope]] = fnContext.AO
3.fn3Context = {
AO: {}
Scope: fn3.[[scope]] = globalContext.AO
}
4.fn2Context = {
AO: {}
Scope: fnContext.AO
}*/