进阶3 函数与作用域

2017-11-29  本文已影响0人  饥人谷_潇湘情绪雨

1.函数声明和函数表达式有什么区别

  //函数声明
  function sayHello(){
    console.log('hello')
  }
  //函数调用
  sayHello()

声明不必放到调用的前面;

  var sayHello = function(){
    console.log('hello');
  }
  sayHello()

声明必须放到调用的前面。

2.什么是变量的声明前置?什么是函数的声明前置

var 和 function 的声明前置
在一个作用域下,var 声明的变量和function 声明的函数会前置
例如:

console.log(a); //undefined
var a = 3;
console.log(a); //3

sayHello();

function sayHello(){
  console.log('hello');
}

执行的实际情况为:

var a;     // 变量声明前置                     
function sayHello() {     // 函数声明前置
    console.log('hello');
}
console.log(a); 
a = 3;
console.log(a);
sayHello();

3.arguments 是什么

在函数内部,你可以使用arguments对象获取到该函数的所有传入参数,
arguments 是一个对应于传递给函数的参数的类数组对象。

 function printPersonInfo() {
   console.log(arguments[0]);
   console.log(arguments[1]);
   console.log(arguments[2]);
 }
printPersonInfo('zz',15 , 'boy');

4.函数的"重载"怎样实现

在 JS 中没有重载,同名函数会覆盖。 但可以在函数体针对不同的参数调用执行相应的逻辑。

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.立即执行函数表达式是什么?有什么作用

(function(){/* code */})()

(function(){/* code */}())

6.求n!,用递归来实现

function factor(n){
  if(n === 0 ||n === 1) {
    return 1;
  }
  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, '男');
getInfo('小谷', 3);
getInfo('男');

输出结果:

name:饥人谷
age:2
sex:男
["饥人谷", 2, "男"]
name:valley
name:小谷
age:3
sex:undefined
[“小谷”, “3”]
name:valley
name:男
age:undefined
sex:undefined
["男"]
name:valley

8.写一个函数,返回参数的平方和?

function sumOfSquares(){
   var sum = 0;
    for(var i = 0; i < arguments.length; i++) {
        sum = 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);
    var a = 1;
    console.log(b);

声明前置实际为:

var a;
console.log(a);  //undefined
a = 1;
console.log(b); //Uncaught ReferenceError: b is not defined

console.log(a); 这个时候a还没有赋值; b变量没有声明和定义, 所以报错.

10.如下代码的输出?为什么

sayName('world');
    sayAge(10);
    function sayName(name){
        console.log('hello ', name);
    }
    var sayAge = function(age){
        console.log(age);
    };

声明前置实际为:

function sayName(name){
  console.log('hello', name);
}
var sayAge
sayName('world');   //hello world
sayAge(10);    // Uncaught TypeError: sayAge is not a function
sayAge = function(age){
  console.log(age)
};

函数声明在作用域内会前置, 函数表达式不会前置,所以函数表达式需要放在调用的前面,后一个函数调用的时候还未被定义,所以出现报错。

11. 如下代码输出什么? 写出作用域链查找过程伪代码

var x = 10
bar() 
function foo() {
  console.log(x)
}
function bar(){
  var x = 30
  foo()
}

作用域链查找过程伪代码:

globalContext= {
  AO: {
    x: 10
    foo: funtion
    bar: funtion
         },
        Scope: null
}
//声明 foo 时 得到下面
foo.[[scope]] = globalContext.AO
//声明 bar 时 得到下面
bar.[[scope]] = globalContext.AO
当调用 bar() 时, 进入 bar 的执行上下文
barContext = {
  AO: {
    x: 30
  },
  Scope: bar.[[scope]] //globalContext.AO
}
当调用 foo() 时,先从 bar 执行上下文中的 AO里找,找不到再从 bar 的 [[scope]]里找到后即调用
当调用 foo() 时,进入 foo 的执行上下文
fooContext = {
  AO: {},
  Scope: foo.[[scope]] // globalContext.AO
}
所以 console.log(x)是 10

12.如下代码输出什么? 写出作用域链查找过程伪代码

var x = 10;
bar() 
function bar(){
  var x = 30;
  function foo(){
  console.log(x) 
  }
  foo();
}       

作用域链查找过程伪代码:

globalContext = {
  AO: {
    x: 10
    bar: function
  },
  Scope: null
}
//声明 bar 时 得到下面
bar.[[scope]] = globalContext.AO
当调用 bar() 时, 进入 bar 的执行上下文
barContext = {
  AO: {
    x: 30,
    foo: function
  },
  Scope: bar.[[scope]] //globalContext.AO
}
//在 bar 的执行上下文里声明 foo 时 得到下面
foo.[[scope]] = barContext.AO
当调用 foo() 时,先从 bar 执行上下文中的 AO里找,找到后即调用
当调用 foo() 时,进入 foo 的执行上下文
fooContext = {
  AO: {},
  Scope: foo.[[scope]] // barContext.AO
}
所以 console.log(x)是 30

13.以下代码输出什么? 写出作用域链的查找过程伪代码

var x = 10;
bar() 
function bar(){
  var x = 30;
  (function (){
    console.log(x)
  })()
}

作用域链查找过程伪代码:

globalContext = {
  AO: {
    x: 10,
    bar: function
  },
  Scope: null
}
//声明bar时
bar.[[scope]] = globalContext.AO
//调用bar时,进入bar的执行上下文
barContext = {
  AO: {
    x: 30,
    function//立即执行函数表达式
  },
  Scope: bar.[[scope]] // globalContext.AO
//执行立即执行函数
functionContext = {
  AO:{ }
},
Scope: function.[[scope]] //barContext.AO
所以console.log(x)为30。

14.以下代码输出什么? 写出作用域链查找过程伪代码

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)

作用域链查找过程伪代码:

globalContext = {
  AO: {
    a: 1,
    fn: function,
    fn3: function
  },
  Scope: null
}
//声明fn时
fn.[[scope]] = globalContext.AO
//声明fn3时
fn3.[[scope]] = globalContext.AO
调用fn,进入fn的执行上下文
fnContext = {
  AO: {
    a: 5
    fn2: function
  },
  Scope: fn.[[scope]] //globalContext.AO
}
//调用fn3,此时在fn的执行上下文的AO中找不到fn3,于是在fn的scope里找,找到后即调用
fn3Context = {
  AO: {
    a: 200
  },
  Scope: fn3.[[scope]] //globalContext.AO
}
//调用fn2,在bar的执行上下文AO中可以找到fn2,直接调用
fn2Context = {
AO: {
    a: 20
  },
  Scope: fn2.[[scope]] //fnContext.AO
}
//输出结果如下:
//调用fn
console.log(a) // a的值在fnContext.AO中,此时a变量声明了但未定义,所以输出undefined。
var a = 5 // a被赋值为5
console.log(a) // 输出5
a++ // fnContext.AO中a的值变为6
//调用fn3
fn3() //此时a的值为globalContext.AO里的a,即console.log(a) 输出1
a = 200 // globalContext.AO里的a值变为200
//调用fn2
fn2() //此时a的值为fnContext.AO里的a,即console.log(a) 输出6
a = 20 // fnContext.AO里的a变为20
console.log(a) //输出20
//fn调用结束,此时 globalContext.AO里的a值为200
console.log(a) // 输出200
// 输出顺序为:undefined, 5, 1, 6, 20, 200
上一篇 下一篇

猜你喜欢

热点阅读