函数与作用域

2017-11-03  本文已影响0人  cctosuper

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

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

var a = 1;
function main() {
    console.log(a);
    var a = 2;
}
main()//输出undefined
解析如下:

var a = 1;
function main() {
    var a;        //这时的a是undefined
    console.log(a);
    a = 2;
}

arguments 是什么

函数的"重载"怎样实现

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');

立即执行函数表达式是什么?有什么作用

//正确的写法:
  ( function(){ /* code */ }() );
  // 或者
  ( function(){ /* code */ } )();
[function fn(){var a=3}]()
,function fn(){var a=3}()
!function fn(){var a=3}()

求n!,用递归来实现

function recursion(n) {
  if (n === 1 || n === 0) {
    return 1;
  }
  else if (n<0) {
    return console.log("minus has no recursion");
  }
  return n*recursion(n-1);
}
console.log(recursion(8));

以下代码输出什么?

    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: "男"
       Arguments["饥人谷", 2, "男"]
       name: ”valley“
   */

    getInfo('小谷', 3);
    /*name:  "小谷"
       age: 3
       sex: undefined
       Arguments["小骨", 3]
       name: ”valley“
   */

    getInfo('男');
    /*name:  "男"
       age: undefined
       sex: undefined
       Arguments["男"]
       name: ”valley“
   */

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

   function sumOfSquares(){
     var result= 0;
     for (var i=0; i<arguments.length; i++) {
       result+= arguments[i]* arguments[i];
     }
     return result;
   }
   var result = sumOfSquares(2,3,4)
   var result2 = sumOfSquares(1,3)
   console.log(result)  //29
   console.log(result2)  //10

如下代码的输出?为什么

    console.log(a); //undefined, 变量声明会前置,并赋值undefined
    var a = 1;
    console.log(b); //报错, b未声明

如下代码的输出?为什么

    sayName('world');
    sayAge(10);
    function sayName(name){
        console.log('hello ', name);
    }
    var sayAge = function(age){
        console.log(age);
    }; //输出hello  world,同时报错sayAge is not a function。因为这是一个函数表达式,声明必须放到调用的前面,而这里声明放在了后面。

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

var x = 10
bar() 
function foo() {
  console.log(x)
}
function bar(){
  var x = 30
  foo()
} //输出 10
/*
globalContent= {
  A0 {
       x: 10
       foo: function
       bar: function
  }
  scope: null
}
foo[[scope]] = globalContent.A0
bar[[scope]] = globalContent.A0
barContent= {
  A0 {
       x: 30
  }
  scope: globalContent.A0
}
fooContent= {
  A0 {}
  scope: globalContent.A0
}
*/

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

var x = 10;
bar() 
function bar(){
  var x = 30;
  function foo(){
    console.log(x) 
  }
  foo(); 
} // 输出30
/*
globalContent= {
  A0 {
    x: 10
  }
  scope: null
}
bar[[scope]] = globalContent.A0
barContent= {
  A0 {
    x: 30
    foo: function
  }
  scope: globalContent.A0
}
fooContent[[scope]] = barContent.A0
fooContent = {
  A0 {}
  scope :barContent.A0
}
*/

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

var x = 10;
bar() 
function bar(){
  var x = 30;
  (function (){
    console.log(x)
  })()
} //输出30
/*
globalContent = {
  A0 {
    x: 10
    bar: function
  }
  scope: null
}
bar[[scope]] = globalContent.A0
barContent = {
  A0 {
    x: 30
    function()
  }
  scope: globalContent.A0
}
function()[[scope]] = barContext.AO
functionContent = {
  A0 {}
  scope: barContext.AO
}
*/

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

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
/*
1. globalContext = {
         A0 {
           a: 1
           fn: function
           fn3: function
         }
         scope: null
       }
       fn[[scope]] = globalContext.A0
       fn3[[scope]] = globalContext.A0
2. fnContent = {
      A0 {
        a: undefined, 5, 6
        fn2: function
      }
     scope: globalContext.A0
   }
   fn2[[scope]] = fnContext.A0
3. fn2Content = {
      A0 {}
      scope: fnContext.A0 
    }
4. fn3Content = {
    A0 {}
    scope: globalContext.A0
    }   
*/
上一篇下一篇

猜你喜欢

热点阅读