task3

2017-12-21  本文已影响0人  ngzk46

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

函数声明

function functionName(){
    statement;
}

函数声明不必放到调用之前

函数表达式

var printName = function(){
    statement;
};

声明必须放到调用之前

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

变量的声明前置
在解析代码时,变量会被提到当前作用域开头来声明,然后运行到原来的位置时再赋值。在赋值之前的调用会返回undefined而不是出错。
函数的声明前置
函数声明也会被提到当前作用域开头来声明,这样可以在把声明放到调用之后。

3.arguments 是什么

在JavaScript中,arguments是对象的一个特殊属性。arguments对象就像数组,但是它却不是数组,而是一个Arguments对象。arguments有length属性。

function printPersonInfo(name, age, sex){
    console.log(name);
    console.log(age);
    console.log(sex);
    console.log(arguments[0]);
}
printPersonInfo("a","b","c");
a
b
c
a

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

根据传入参数的不同执行不同的逻辑

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

部分转自
立即执行函数能够立即执行,这样可以做到隔离作用域,避免变量污染全局。

var liList = ul.getElementsByTagName('li')
for(var i=0; i<6; i++){
  liList[i].onclick = function(){
    alert(i)      // 6
  }
}

为什么 alert 出来的总是 6,而不是 0、1、2、3、4、5因为 i 是贯穿整个作用域的,而不是给每个 li 分配了一个 i

image

那么怎么解决这个问题呢?用立即执行函数给每个 li 创造一个独立作用域即可(当然还有其他办法):

var liList = ul.getElementsByTagName('li')
for(var i=0; i<6; i++){
  !function(ii){
    liList[ii].onclick = function(){
     alert(ii) // 0、1、2、3、4、5
      }
  }(i)
} 

在立即执行函数执行的时候,i 的值被赋值给 ii,此后 ii 的值一直不变。
i 的值从 0 变化到 5,对应 6 个立即执行函数,这 6 个立即执行函数里面的 ii 「分别」是 0、1、2、3、4、5。

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

function fac(n){
    if(n===1||n===0){
      return 1;
    }
    return n*fac(n-1);
}
console.log(fac(10));

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

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(var i=0;i<arguments.length;i++){
        sum += arguments[i]* arguments[i];
    }
    return sum;
}
console.log(sumOfSquares(2,5));   //29

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

    console.log(a);    //undefined    已经声明但是没有赋值
    var a = 1;
    console.log(b);    // 报错 未声明b

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

    sayName('world');    // hello world
    sayAge(10);    //报错 函数表达式必须在调用前声明
    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
调用过程:
1. globalContext = {
    AO: {
        x: 10;
        foo:function
        bar:function
    }
    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
调用过程:
1. globalContext = {
    AO: {
        x: 10;
        bar:function
    }
    bar.[[scope]] = globalContext.AO
}
2.调用bar()  barContext = {
    AO:{
        x:30;
        foo:function
    }
    scope:bar.[[scope]] = globalContext.AO
    scope: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
    }
    bar.[[scope]] = globalContext.AO
}
2.调用bar()  barContext = {
    AO:{
        x:30;
        匿名:function
    }
    scope:bar.[[scope]] = globalContext.AO
    scope:匿名.[[scope]] = barContext.AO
}
3.调用匿名函数()   匿名Context = {
    AO:{}
    scope:匿名函数.[[scope]] =barContext.AO
}

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

var a = 1;

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

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

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

fn()
console.log(a)  // 200
调用过程:
1. globalContext {
    AO: {
        a: 200;
        fn: function
        fn3: function
    }
    Scope: null;
   fn.[[scope]]=globalContext.AO
   fn3.[[scope]]=globalContext.AO
}
2. 调用fn() 
fnContext{
      AO:{
        a:20
        fn2:function
      }
      fn.[[scope]]=globalContext.AO
      fn2.[[scope]]=fnContext.AO
}
3. 调用fn3()
      fn3Context{
        AO:{}
        fn3.[[scope]]=globalContext.AO
      }
4. 调用fn2()
      fn2Context{
        AO:{}
        fn2.[[scope]]=fnContext.AO
      }
上一篇 下一篇

猜你喜欢

热点阅读