JS-函數與作用域

JavaScript中的函數運行在它們被定義的作用域里,而不是它們被執行的作用域里。

  1. 函數聲明和函數表達式有什么區別
    函數聲明和函數表達式非常相似,有著相同的語法。函數聲明的函數名不能省略。函數表達式的函數名,可以省略。當省略函數名的時候,該函數就成為了匿名函數。
    函數聲明會被整體提升,調用可以放在聲明的前面。
    function a(){}
    而函數表達式是一個表達式,常出現在賦值的式子中。只有聲明變量的部分會被提升,調用要放在聲明的后面
    function (){}
  2. 什么是變量的聲明前置?什么是函數的聲明前置
    變量的聲明前置:所有變量的聲明語句會提升到代碼的頭部
    函數的聲明前置:采用function命令聲明函數時,整個函數會像變量聲明一樣,被提升到代碼頭部
  3. arguments 是什么
    arguments對象在函數內部用來獲取到該函數的所有傳入的參數.arguments對象不是一個數組 。它類似于數組,但除了length之外沒有任何數組屬性
  4. 函數的"重載"怎樣實現
    JS是不支持重載的,第二個同名函數會覆蓋前一個。但是可以通過arguments對象來實現“重載”。
    實現重載常用的方式:
    1、根據傳入參數的類型執行不同的操作。
    2、利用參數中特殊的參數值進行不同的操作。
    3、根據參數的個數進行重載。
  5. 立即執行函數表達式是什么?有什么作用
    立即執行函數表達式常見兩種形式
    (function (){console.log(123)}())
    (function (){console.log(123)})()
    立即執行函數可以創建一個獨立的作用域,防止變量污染。
  6. 求n!,用遞歸來實現
function a(n){
  if(n>1){
     return n * a(n-1) 
  }
  if(n===1){
    return 1 
  }
}

代碼題

  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, '男');// name:饑人谷 age:2 sex:男 ['饑人谷', 2, '男'] name valley
getInfo('小谷', 3); //name: 小谷 age:3 sex:undefined ["小谷", 3] name valley
getInfo('男'); //name:男 age: undefined sex: undefined ["男"] name valley

8. 寫一個函數,返回參數的平方和?

function sumOfSquares(){
var sum = 0
for(i=0;i<arguments.length;i++){
sum += Math.pow(arguments[i],2)
}
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  變量提升
var a = 1;           
console.log(b);   // b is not defined    
10. 如下代碼的輸出?為什么

sayName('world'); //hello world 函數聲明提升
sayAge(10); //sayAge is not a function 變量提升 函數表達式不提升
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};

11. 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼

var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
}
輸出10
下面是偽代碼

globalContext = {
AO: {
x: 10
foo: function
bar: function
}
Scope: null
}
foo.[[scope]] = globalContext.AO
bar.[[scope]] = globalContext.AO
2.調用bar()
barContext = {
AO: {
x: 30
}
Scope: bar.[[scope]] = globalContext.AO
}
3.調用foo()
fooContext = {
AO: {}
Scope: foo.[[scope]] = globalContext.AO

12. 如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼

var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
輸出30
下面是偽代碼

globalContext = {
AO: {
x: 10
bar: function
}
Scope: null
}
bar.[[scope]] = globalContext.AO
2.調用bar()
barContext = {
AO: {
x: 30
foo: function
}
Scope: bar.[[scope]] = globalContext.AO
}
foo.[[scope]] = barContext.AO
3.調用foo()
fooContext = {
AO: {}
Scope: foo.[[scope]] = barContext.AO
}

13. 以下代碼輸出什么? 寫出作用域鏈的查找過程偽代碼

var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}
輸出 30
下面是偽代碼
1 .
globalContext = {
AO: {
x: 10
bar: function
}
Scope: null
}
bar.[[scope]] = globalContext.AO
2.調用bar()
barContext = {
AO: {
x: 30
function(){}
}
Scope: bar.[[scope]] = globalContext.AO
}
function.[[scope]] = barContext.AO
3.調用function()
functionContext = {
AO: {}
Scope: function.[[scope]] = barContext.AO
}

14.  以下代碼輸出什么? 寫出作用域鏈查找過程偽代碼

var a = 1;

function fn(){
console.log(a) //重寫過程 var a
var a = 5 //console.log(a) a = undefined
console.log(a) //a=5
a++ //console.log(a) a = 5
var a //a++
fn3() // a = 1
fn2() // a = 6
console.log(a)// a = 20

    function fn2(){
      console.log(a)     //         6
      a = 20
    }

}

function fn3(){
console.log(a) // 1
a = 200
}

fn()
console.log(a) // a = 200

下面是偽代碼

globalContext = {
AO: {
a = 1
fn : function
fn3 : function
}
Scope : null
}
fn.[[Scope]] = globalContext.AO
fn3.[[Scope]] = globalContext.AO
2.調用fn
fnContext = {
AO:{
a = undefined 5 6 20
fn2 = function
}
Scope : fn.[[Scope]] = globalConstext.AO
}
fn2.[[Scope]] = fnContext.AO
3.調用fn3
fn3Context = {
AO:{

    }
    Scope : fn3.[[Scope]] = globalContext.AO

}
4.調用fn2
fn2Context = {
AO: {

    }
     Scope: fn2.[[scope]] = fnContext.AO

}
5.console.log(a) globalContext.AO

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 函數聲明和函數表達式有什么區別? 函數聲明和函數表達式是EMACScript規定的兩種不同的聲明函數的方法。1.函...
    LeeoZz閱讀 354評論 0 1
  • 1.函數聲明和函數表達式有什么區別 函數聲明可以看作是函數的初始化,我們將給函數傳參并建立函數體的表達式,當我門建...
    高進哥哥閱讀 226評論 0 0
  • 函數聲明和函數表達式有什么區別 JavaScript 中需要創建函數的話,有兩種方法:函數聲明、函數表達式,各自寫...
    蕭雪圣閱讀 968評論 2 2
  • 1. 函數聲明和函數表達式有什么區別 使用function關鍵字聲明一個函數時,聲明不必放到調用的前面。//函數聲...
    _李祺閱讀 281評論 0 0
  • 1、你會不會因為自己身邊的某個熟人因為做了一件令自己討厭的事情而開始嫌棄他,甚至覺得再也找不到他的優點,從而掙扎在...
    樂L魚Y兒E閱讀 500評論 0 0