函数与作用域

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

   函数声明:function functionName(){}    声明不必放到调用的前面,可放随意位置,随时调用
  函数表达式:var fn = function(){}       声明必须放到调用的前面

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

所谓的变量声明前置就是在一个作用域块中,在代码执行前会先将所有声明变量提前进行初始化再执行语句。和变量声明前置一样,执行代码之前会先读取函数声明,只要函数在代码中进行了声明,无论它在哪个位置上进行声明,js引擎都会将它的声明放在作用域的顶部。

3.arguments 是什么

arguments是一个类数组对象。代表传给function的参数列表。
arguments对象是函数内部的本地变量;arguments 已经不再是函数的属性了。可以在函数内部通过使用 arguments 对象来获取函数的所有参数。这个对象为传递给函数的每个参数建立一个条目,条目的索引号从0开始。它包括了函所要调用的参数。object对象。类数组。

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(){
  var a  = 1;
})()
console.log(a); //undefined

作用: 隔离作用域。

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

function factor(n){
  if(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, '男');
/*
  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
*/

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

function sumOfSquares(){
        var sum=0;
        for(var i=0;i

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()
}
/*    1.
    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
    }
    x变成30

    3.调用foo()
    fooContext = {
        AO:{},
        Scope:foo.[[scope]] = globalContext.AO
    }
    输出10
*/


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

var x = 10;
bar() 
function bar(){
  var x = 30;
  function foo(){
    console.log(x) 
  }
  foo();
}
/*    1.
    globalContext = {
        AO:{
            x:10
            bar:function
        },
        Scope:null
    }

    bar.[[scope]] = globalContext.AO
    2.调用bar()
    barContext = {
        AO:{
          x:30
        },
        Scope:bar.[[scope]] // globalContext.AO
    }
    foo.[[scope]] = barContext.AO
    3.调用foo()
    fooContext = {
        AO:{},
        scope:foo.[[scope]]
    }
    输出结果为30
*/

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

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

 /*
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.调用立即执行函数
    functionContext = {
        AO:{},
        Scope:function.[[scope]]//barContext.AO
    }
    结果为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)


/*
    1.
    globalContext = {
        AO:{
            a:1
            fn:function
            fn3:function
        },
        Scope:null
    }
    fn.[[scope]] = globalContext.AO
    fn3.[[scope]] = globalContext.AO

    2.调用fn()
    fnContext = {
        AO:{
            a:undefined
            fn2:function
        },
        Scope:fn.[[scope]] // globalContext.AO
    }
    fn2.[[scope]] = fnContext.AO

    3.
    fn3Context = {
        AO:{
            a:200
        },
        Scope:fn3Context.[[scope]]//globalContext.AO
    }
    fn2ConText = {
        AO:{
            a:20
        },
        Scope:fn2ConText.[[scope]]//fnContext.AO
    }



    开始执行
    console.log(a)//undefined 打印
    var a = 5//fnContext中a变成5
    console.log(a)//5
    a++//fnContext.AO中a变为6
    调用fn3()
    fn3()中
    console.log(a)//globalContext.AO中的a值为1,打印
    a = 200//globalContext.AO中的a变为200
    调用fn2()
    console.log(a);//fnContext.AO中的a值为6,打印
    a = 20;//fnContext.AO中的a变为20
    继续执行fn()
    console.log(a)//fnContext.AO中的a值为20,打印
    fn()结束
    console.log(a)//globalContext.AO中a值为200,打印

    //输出的结果 undefined 5 1 6 20 200
*/


你可能感兴趣的:(函数与作用域)