进阶3

2017-09-12  本文已影响0人  饥人谷_星璇

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

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

3.arguments 是什么

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

  function printPeopleInfo(name, age, sex){
    if(name){
      console.log(name);
    }

    if(age){
      console.log(age);
    }

    if(sex){
      console.log(sex);
    }
  }

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

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

function fn(n){
    if (n === 1){
        return 1;
}
   return n*fn(n-1);
}
   fn(n)

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:男 age:undefined sex:undefined  ['男']  name:valley

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

   function sumOfSquares(){
        if (arguments.length === 0){
             alert("请输入至少一个参数")
         }
         var sum = 0;
       for(var i=0;i<arguments.length;i++){
                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);  //undefined
    var a = 1;
    console.log(b);   // 报错

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()  // x= 10
function foo() {
  console.log(x)
}
function bar(){
  var x = 30
  foo()
}
==>
/*
globalContext = {
        AO:{
               x:10;
               foo: function(){}
               bar: function(){}
                }
               scope:null;
            }
barContext = {
      AO:{
            x:30;
         }
      bar.[[scope]]  =globalContext.AO
}
fooContext = {
      AO:{
         }
      foo.[[scope]]  =globalContext.AO
}
*/

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

var x = 10;
bar()       //  30
function bar(){
  var x = 30;
  function foo(){
    console.log(x) 
  }
  foo();
}   
==>
/*
globalContext = {
        AO:{
               x:10             
               bar: function(){}
                }
               scope:null;
            }
barContext = {
      AO:{
            x:30;
            foo: function(){}
         }
      bar.[[scope]]  =globalContext.AO
}
fooContext = {
      AO:{
         }
      foo.[[scope]]  =barContext.AO
}
*/
  1. 以下代码输出什么? 写出作用域链的查找过程伪代码
var x = 10;
bar()          // 30
function bar(){
  var x = 30;
  (function (){
    console.log(x)
  })()
}
==>
/*globalContext = {
  AO: {
    x: 10
    bar: function
  }
  Scope: null
}

barContext = {
  AO: {
    x: 30
    function: function
  }
  Scope: globalContext.AO
}

functionContext = {
  AO: {}
  Scope: barContext.AO
}
*/

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

var a = 1;

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

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

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

fn()        // undefined  5   1  6  20
console.log(a)   // 200
==>
/*
globalContext = {
  AO: {
    x: 1
    fn: function
    fn3:function
  }
  Scope: null
}
fnContext = {
  AO: {
    a: undefined
    a:5
    a:6
    fn2:function
  }
  Scope: globalContext.AO
}
fn2Context = {
  AO: {
    a:20
  }
  Scope: fnContext.AO
}
fn3Context = {
  AO: {
    a:200
  }
  Scope: globalContext.AO
}
*/
上一篇 下一篇

猜你喜欢

热点阅读