美文网首页
getability---three

getability---three

作者: Klart | 来源:发表于2017-08-05 01:15 被阅读7次

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

函数声明:function name(){}
函数表达式:var fn=function(){}
函数声明会提前,函数表达式可以省略标识符(函数名)

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

变量声明前置:在一个作用域块中,变量被放在块的最开始处声明,如无初始值,为undefined;
函数声明前置:与变量声明前置一样;使用函数声明的函数定义方法,函数调用可以放在任意位置,函数声明是在预执行期执行的,就是说函数声明是在浏览器准备执行代码的时候执行的,因为函数声明是在预执行期被执行,所以到了执行期就不再执行了。

3:arguments是什么?

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

4:函数的“重载怎样实现”?

JS中没有重载,相同函数名会被后者覆盖;
重载:相同函数名、相同参数、不同类型;
例:function name(int x,int y);function name(string x,string y);

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

立即执行函数:(function(){})();
作用:隔离作用域;

6:求n!,用递归来实现;

function sums(n){ if(n===1){return 1;}return n*sums(n-1);}
sums(3)//6

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 result = sumOfSquares(2,3,4)
   var result2 = sumOfSquares(1,3)
   console.log(result)  //29
   console.log(result2)  //10
答案:
sumOfSquares(){
      var sum=0;
        for(var i=0;i<arguments.length;i++){
            sum=sum+arguments[i]*arguments[i];
        }
        console.log(sum);
}

9. 如下代码的输出?为什么
    console.log(a);
    var a = 1;
    console.log(b);
答案:undefined ,b is not defind//没有声明这个变量

10. 如下代码的输出?为什么
    sayName('world');
    sayAge(10);
    function sayName(name){
        console.log('hello ', name);
    }
    var sayAge = function(age){
        console.log(age);
    };
答案:hello world , 报错//函数表达式不会前置;

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
*/

相关文章

  • getability---three

    1:函数声明和函数表达式有什么区别? 函数声明:function name(){}函数表达式:var fn=fun...

网友评论

      本文标题:getability---three

      本文链接:https://www.haomeiwen.com/subject/bkqilxtx.html