進(jìn)階任務(wù)3

1.函數(shù)聲明和函數(shù)表達(dá)式有什么區(qū)別
函數(shù)表達(dá)式的聲明必須放在調(diào)用前面,函數(shù)聲明的聲明不需要放在調(diào)用前面。
2.什么是變量的聲明前置?什么是函數(shù)的聲明前置
在一個(gè)作用域下,var 聲明的變量和function 聲明的函數(shù)會(huì)前置

console.log(a)
var a=1 //由于var聲明提前,輸出結(jié)果為undefined
sayHello();
function sayHello(){console.log('hello')} //輸出為hello

3.arguments 是什么
arguments是函數(shù)的一個(gè)內(nèi)置參數(shù)的數(shù)組對(duì)象,通過(guò)arguments[1、2、3]等...我們可以獲取到相應(yīng)的傳入?yún)?shù).
4.函數(shù)的"重載"怎樣實(shí)現(xiàn)
在 JS 中沒(méi)有重載! 同名函數(shù)會(huì)覆蓋。 但可以在函數(shù)體針對(duì)不同的參數(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);
  printPeopleInfo('Byron', 26, 'male'); 

5.立即執(zhí)行函數(shù)表達(dá)式是什么?有什么作用
立即執(zhí)行函數(shù)表達(dá)式(Immediately-Invoked Function Expression),簡(jiǎn)稱IIFE。表示定義函數(shù)之后,立即調(diào)用該函數(shù)。

(function(){var a=1;})()
[function(){var a=1;}]()
1,function(){var a=1;}

作用是隔離作用域
6.求n!,用遞歸來(lái)實(shí)現(xiàn)

function(n){
if(n===1){return 1}
return n*function(n-1)
}
function getInfo(name, age, sex){
        console.log('name:',name); //輸出name
        console.log('age:', age); //輸出age
        console.log('sex:', sex); //輸出sex
        console.log(arguments); //輸出整個(gè)arguments
        arguments[0] = 'valley'; //arguments第一項(xiàng)變?yōu)関alley
        console.log('name', name); 輸出arguments[0],即valley
    }
    getInfo('饑人谷', 2, '男'); //輸出name:饑人谷 age:2 sex:男 ["饑人谷," 2 ",男"] name:valley
    getInfo('小谷', 3); //輸出name:小谷 age:3 sex:undefined ["小谷", 3] name:valley
    getInfo('男'); //輸出name:undefined age:undefined sex:男 ["男",] name:valley
function sumOfSquares(){
   var result = 0;
   for(var i = 0;i<arguments.length;i++{
   result  += arguments[i]*arguments[i]; }
   return  result;
}
console.log(a); //輸出undefined,var a前置,然后執(zhí)行console.log(a); 后執(zhí)行a=1
var a = 1;
console.log(b); //error b未被賦值
sayName('world');
sayAge(10);
function sayName(name){
console.log('hello ', name);} //輸出 hello world 聲明前置,然后調(diào)用輸出hello world
var sayAge = function(age){
console.log(age); }; //error 函數(shù)表達(dá)式的調(diào)用在聲明前無(wú)法執(zhí)行
var x = 10
bar() 
function foo() {
  console.log(x)
}
function bar(){
  var x = 30
  foo()
}
// 
1.globalContext={
AO:{x:10
       foo:function
       bar:function
}
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
} 
//
輸出為10
var x = 10;
bar() 
function bar(){
  var x = 30;
  function foo(){
    console.log(x) 
  }
  foo();
}   
// 
1.globalContext={
AO:{x:10
bar:function
}
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
}
//
輸出為30
var x = 10;
bar() 
function bar(){
  var x = 30;
  (function (){
    console.log(x)
  })()
}
//
1.globalContext={
AO:{x:10
bar:function
}
bar.[[scope]]=globalContext.AO
}
2.barContext={
AO:{x:30
function(){}
}
Scope:bar.[[scope]]=globalContext.AO
function.[[scope]]=barContext.AO
}
3.functionContext={
AO:{}
Scope:function.[[scope]]=barContext.AO
}
//
輸出為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
}
fn.[[scope]]=globalContext.AO
fn3.[[scope]]=globalContext.AO
}
2.fnContext={
AO:{a:5 -> 6
fn2:function
}
Scope:fn.[[scope]]=globalContext.AO
fn2.[[scope]]=fnContext.AO
}
3.fn2Context={
AO:{}
Scope:fn2.[[scope]]=fnContext.AO
}
4.fn3Context={
AO:{}
Scope:fn3.[[scope]]=globalContext.AO
}
//
輸出undefined 5 1 6 20 200
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容