函數(shù)聲明和函數(shù)表達(dá)式有什么區(qū)別
用函數(shù)聲明創(chuàng)建的函數(shù)可以在定義之前就進(jìn)行調(diào)用(聲明前置);而用函數(shù)表達(dá)式創(chuàng)建的函數(shù)不能在被賦值之前進(jìn)行調(diào)用(會返回錯誤驱显,因為前置只定義了變量undefined话瞧,不能在undefined上進(jìn)行函數(shù)調(diào)用)。
什么是變量的聲明前置?什么是函數(shù)的聲明前置
在一個作用域下凳忙,var 聲明的變量和function 聲明的函數(shù)會前置
比如var a=3,會前置聲明變量a=undefined,直到運(yùn)行到 var a=3才會賦值3給a(函數(shù)表達(dá)式前置聲明的是var變量undefined,并不是函數(shù)禽炬,所以不能調(diào)用函數(shù))
function a (){} ,會前置聲明a: function涧卵。
arguments 是什么
arguments對象是所有函數(shù)中可用的局部變量。使用arguments對象在函數(shù)中引用函數(shù)的參數(shù)腹尖。此對象包含傳遞給函數(shù)的每個參數(shù)的條目柳恐,第一個條目的索引從0開始
函數(shù)的"重載"怎樣實現(xiàn)
可以根據(jù)arguments對象的length值進(jìn)行判斷
function peopleInfo (name,age,sex){
if(arguments.length === 1){
console.log (name);
}
if (arguments.length === 2){
console.log (name+age);
}
if(arguments.length === 3){
console.log (name+age+sex);
}
}
立即執(zhí)行函數(shù)表達(dá)式是什么?有什么作用
聲明一個函數(shù),在后面加上一個小括號乐设,表示立即調(diào)用這個函數(shù)讼庇,作用是隔離作用域
為了兼容JS的語法,還需要另外加些符號(不加的話會報錯)近尚,寫法為
(function a (){}());
!function a (){}();
[function a (){}()];
求n!蠕啄,用遞歸來實現(xiàn)
function factor(n){
if(n === 1) {
return 1
}
return n * factor(n-1)
}
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('男');
/*
name:饑人谷
age:2
sex:男
饑人谷,2戈锻,男
name valley
name:小谷
age:3
小谷歼跟,3
name valley
name: 男
name valley
*/
function sumOfSquares(numb1,numb2,numb3){
if (arguments.length === 3){
console.log(numb1*numb1+numb2*numb2+numb3*numb3)
}
if(arguments.length ===2){
console.log(numb1*numb1+numb2*numb2)
}
}
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); //undefined 變量a未賦值 , 報錯 沒有變量b
sayName('world');
sayAge(10);
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
}; //hello,world 舶沛, 報錯 sayAge是一個變量無法調(diào)用函數(shù)
var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
}//10
/*
1. globalContext = {
AO: {
x: 10
foo: function
bar: function
},
Scope: null
}
foo.[[scope]] = globalContext.AO
bar.[[scope]] = globalContext.AO
2. barContent={
AO: {
x:30
}
bar.[[scope]] = globalContext.AO
}
3. fooContent={
AO:{}
foo.[[scope]] = globalContext.AO
}
*/
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
} //30
/*
1. globalContext = {
AO: {
x: 10
bar: function
},
Scope: 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]] // globalContext.AO
}
*/
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}// 30
1. globalContent ={
AO:{
x:10
bar:function
}
}
bar[[scope]]=globalContent.AO
2. barContent ={
AO:{
x: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)//undefined 5 1 6 20 200
/*
globalContent
a:200
fn:function
fn3:function
---------------
fnContent
a:20
fn2:function
---------------
fn3Content
---------------
fn2Content
*/