1.函數聲明和函數表達式有什么區別
構造函數常用的兩種方法:函數聲明和函數表達式
//函數聲明
function sum(a, b) {
return a + b;
}
alert(sum(1, 2));
//函數表達式 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)); //以上兩種都可以
這二者區別是:
可以看到函數聲明必須有函數名,而函數表達式中的函數名可以忽略
由于JavaScript解析器對這兩種定義方式讀取的順序不同所以用函數聲明定義的函數,函數可以在函數聲明之前調用,而用函數表達式定義的函數只能在聲明之后調用。
2.什么是變量的聲明前置?什么是函數的聲明前置
變量聲明前置是將變量的聲明提到當前作用域的開頭,比如:
console.log(a); //undefined
var a = 1;
console.log(a); // 1
// 上面的代碼在函數解析的過程是這樣的
var a;
console.log(a); // undefined
a = 1 ;
console.log(a); // 1
變量聲明前置只是將其提前聲明,對于它的賦值則還在原來的位置。
函數的聲明前置,舉個栗子:
fun1(); // 123
function fun1(){
console.log(123);
}
函數聲明前置是將函數聲明提前到當前作用域開頭,注意函數聲明前置比變量聲明前置優先級更高,可以在當前作用域下任何地方調配此函數,區別于函數表達式所必需遵守的先后順序。
3.arguments 是什么
arguments是一個類似數組的對象, 對應于傳遞給函數的參數。
arguments 對象僅在函數內部有效,在函數外部調用 arguments 對象會出現一個錯誤。
可以使用arguments對象在函數中引用函數的參數。此對象包含傳遞給函數的每個參數的條目,比如:
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.函數的"重載"怎樣實現
函數重載是指:形參不同的多個同名函數根據處理數據的不同而返回不同的結果。
函數重載主要運用于c++,java等強類型語言中,因為JavaScript是若類型語言,構建同名函數會把之前的覆蓋掉,因此在JS中沒用重載,但是可以運用一些技巧達到重載的效果。比如:
function fun1(obj) { alert(1) }
function fun1(obj, obj1, obj2) { alert(3) }
function fun1(obj2,obj3) {alert(2) }
fun1();
這樣的代碼在JS中只會彈出“2”,因為后面的函數會覆蓋掉前面的同名函數,那要怎么樣才能打到想要的效果呢,這就需要添加判定條件了
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.立即執行函數表達式是什么?有什么作用
立即執行函數模式是一種語法,可以讓你的函數在定義后立即被執行,作用是隔離作用域,獨立起來不影響全局
(function () {
alert('hello world');
})()
6.求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.寫一個函數,返回參數的平方和?
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
10. 如下代碼的輸出?為什么
sayName('world'); // hello world 函數聲明前置,可以隨意調用
sayAge(10); // 報錯,因為此函數必須必須在其函數表達式之后調用
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
}*/