美文网首页
进阶3作业

进阶3作业

作者: 饥人谷_醉眼天涯 | 来源:发表于2017-09-12 11:21 被阅读0次
1、函数声明和函数表达式有什么区别

函数声明: 使用function关键字可以声明一个函数

function sayHello() {
    console.log('hello');
}
sayHello()

声明不必放到调用前面。

var sayHello = function() {
    console.log('hello');
}
sayHello();

声明必须放到调用的前面。

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

在一个作用域下,var声明的变量和funciton声明的函数会前置。
举列说明

console.log(a);            
var a = 3;           
console.log(a);             
sayHello();                 
function sayHello() {
    console.log('hello');
}

在JS实际执行的时候,是如下面这样。

var a;                              // 变量声明前置
function sayHello() {       // 函数声明前置
    console.log('hello');
}
console.log(a); 
a = 3;
console.log(a);
sayHello();
3、arguments 是什么?

在函数内部,你可以使用arguments对象获取到该函数的所有传入参数。
arguments 叫做类数组对象。 但是它表示数组,可以通过下标的方式获取到值,也有length属性。
举列说明

function printPersonInfo() {
    console.log(arguments[0]);
    console.log(arguments[1]);
    console.log(arguments[2]);
    console.log(arguments.length);
} 
printPersonInfo('Ty', 22, 'female');
4、函数的"重载"怎样实现?

在其他语言中的重载,就是可以多个同名函数,但是参数和返回值类型都不同,但是调用的时候我们可以根据,调用时传参的方式,自动匹配相对应的函数。
在JS中没有重载,同名函数会覆盖,但可以在函数体针对不同的参数调用执行相应的逻辑。
在JS中模拟重载。

function printPersonInfo(name, age, sex) {
    if(name) {
        console.log(name);
    }
    if (age) {
        console.log(age);
    }
    if(sex) {
        console.log(sex);
    }
} 
printPersonInfo('HR', 22);
printPersonInfo('Ty', 22, 'female');

重载就是根据传递的参数不同,做什么样的事情。

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

是一个立即执行的匿名函数表达式,两对括号包裹着一个匿名函数,使匿名函数变成了一个函数表达式。
(function() { var a = 1;})(); 作用: 隔离作用域 举列

(function b() {
      var a = 2
} ) 
console.log(a)   // a is not defined
6、求n!,用递归来实现
function fctoril(n) {
    if(n == 1) { 
        return 1;
    }else {
        return n * fctoril(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, '男');
getInfo('小谷', 3);
getInfo('男');

name: 饥人谷
age: 2
sex: 男
["饥人谷", 2, "男"]
name valley
name: 小谷
age: 3
sex: undefined
["小谷",3]
name valley
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
function sumOfSquares(){
    var sum = 0;
    for(var i = 0; i < arguments.length; i++) {
        sum = sum + arguments[i] * arguments[i];
    }
    return sum;
}
9、如下代码的输出?为什么
console.log(a);
    var a = 1;
    console.log(b);

变量声明前置

var a;
console.log(a);
a = 1;
console.log(b);

输出结果:
undefined
Uncaught ReferenceError: b is not defined
a 先声明了,而b没有声明。

10、如下代码的输出?为什么
sayName('world');
    sayAge(10);
    function sayName(name){
        console.log('hello ', name);
    }
    var sayAge = function(age){
        console.log(age);
    };

Uncaught TypeError: sayAge is not a function
函数声明不需要放在调用调用的前面。
函数表达式需要放在调用的前面

11、如下代码输出什么? 写出作用域链查找过程伪代码
var x = 10
bar() 
function foo() {
  console.log(x)
}
function bar(){
  var x = 30
  foo()
}

输出结果:10

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

输出结果:30

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

输出结果:30

// 作用域链
globalContext = {
    AO: {
        x: 10,
        bar: function() {};
    }
    Scope: null
}
bar.[[scope]] = globalContext.AO;

barContext = {
    AO: {
        x: 30,
         anonymity :function() {}
    }
    Scope:globalContext.AO;
}
anonymity.[[scope]] = barContext.AO;
14、如下代码输出什么? 写出作用域链查找过程伪代码
var a = 1;

function fn(){
  console.log(a)         // (1)
  var a = 5
  console.log(a)         //  (2)
  a++
  var a
  fn3()                         // (3)
  fn2()                         // (4)
  console.log(a)          // (5)      

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

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

fn()
console.log(a)                    // (6)

在上面标注了输出时代码的序号。

// 执行到(1)时对应的作用域链
globalContext = {
    AO:{
        a:1,
        fn: function() {},
        fn3: function() {},
    }
    Scope: null;
}
fn.[[scope]] = globalContext.AO;
fn3.[[scope]] = globalContext.AO;

fnContext = {
    AO: {
        a: undefined,
        fn2: function() {}
    }
    scope: fn.[[scope]] = globalContext.AO;
}
fn2.[[scope]] = fnContext.AO;

此时输出 undefined

// 执行到(2)时对应的作用链
globalContext = {
    AO:{
        a:1,
        fn: function() {},
        fn3: function() {},
    }
    Scope: null;
}
fn.[[scope]] = globalContext.AO;
fn3.[[scope]] = globalContext.AO;

fnContext = {
    AO: {
        a: 5,
        fn2: function() {}
    }
    scope: fn.[[scope]] = globalContext.AO;
}
fn2.[[scope]] = fnContext.AO;

(2)此时结果:undefined 5

// 执行到(3)时对应的作用域链
globalContext = {
    AO:{
        a:1,
        fn: function() {},
        fn3: function() {},
    }
    Scope: null;
}
fn.[[scope]] = globalContext.AO;
fn3.[[scope]] = globalContext.AO;

fnContext = {
    AO: {
        a: 6,
        fn2: function() {}
    }
    scope: fn.[[scope]] = globalContext.AO;
}
fn2.[[scope]] = fnContext.AO;

fn3Content = {
    AO:{}
    scope: fn3.[[scope]] = globalContext.AO;
}

(3)此时结果:undefined 5 1

// 执行到(4)对应......
globalContext = {
    AO:{
        a:200,
        fn: function() {},
        fn3: function() {},
    }
    Scope: null;
}
fn.[[scope]] = globalContext.AO;
fn3.[[scope]] = globalContext.AO;

fnContext = {
    AO: {
        a: 6,
        fn2: function() {}
    }
    scope: fn.[[scope]] = globalContext.AO;
}
fn2.[[scope]] = fnContext.AO;

fn3Content = {
    AO:{}
    scope: fn3.[[scope]] = globalContext.AO;
}

fn2Content = {
    AO: {}
    scope: fn2.[[scope]] = fnContext.AO;
}

(4)此时结果为 undefined 5 1 6

// (5)对应的作用链
globalContext = {
    AO:{
        a:200,
        fn: function() {},
        fn3: function() {},
    }
    Scope: null;
}
fn.[[scope]] = globalContext.AO;
fn3.[[scope]] = globalContext.AO;

fnContext = {
    AO: {
        a: 20,
        fn2: function() {}
    }
    scope: fn.[[scope]] = globalContext.AO;
}
fn2.[[scope]] = fnContext.AO;

fn3Content = {
    AO:{}
    scope: fn3.[[scope]] = globalContext.AO;
}

fn2Content = {
    AO: {}
    scope: fn2.[[scope]] = fnContext.AO;
}

(5)此时结果为undefined 5 1 6 20

//  (6)
globalContext = {
    AO:{
        a:200,
        fn: function() {},
        fn3: function() {},
    }
    Scope: null;
}
fn.[[scope]] = globalContext.AO;
fn3.[[scope]] = globalContext.AO;

fnContext = {
    AO: {
        a: 20,
        fn2: function() {}
    }
    scope: fn.[[scope]] = globalContext.AO;
}
fn2.[[scope]] = fnContext.AO;

fn3Content = {
    AO:{}
    scope: fn3.[[scope]] = globalContext.AO;
}

fn2Content = {
    AO: {}
    scope: fn2.[[scope]] = fnContext.AO;
}

(6)输出结果 : undefined 5 1 6 20 200

相关文章

  • 进阶3作业

    1、函数声明和函数表达式有什么区别 函数声明: 使用function关键字可以声明一个函数 声明不必放到调用前面。...

  • 10.17

    作业1 作业2 作业3 作业4 作业5 作业6 作业7 作业8 作业9 作业10 作业11 思考题一 进阶题一

  • 11.17

    作业1 作业2 作业3 作业4 作业5 作业6 作业7 作业8 作业9 作业10 作业11 思考题一 进阶题一

  • 2020-05-11 塔勒布信息分析报告(第一版)

    Ch2 进阶作业二进阶作业为必做题。本章进阶作业共 2 个,请你至少完成 1 个。 任选一位你感兴趣的学者,参考以...

  • 【海比特03天作业】作业宣言+核心知识点+答疑评估

    版块0☞作业宣言 【海比特作业宣言】我本次完成作业分为三阶段:基础(版块0+1+2)、进阶(版块3+4)、高级(版...

  • SQL语句21天打卡,第3天0330

    2020年03月30日软件测试圈「每日进阶」 第3天作业 , 根据已有的表istester 和 idoxu 创建新...

  • Apr. one day 执行-20.04.07

    【7号完成的事】 1,早起; 2,成功日记; 3,进阶课内容梳理、晚读、交作业、统计学分; 4,吃苹果 5,追剧《...

  • Fragment使用三部曲

    1.Fragment进阶 - 基本用法2.Fragment进阶-FragmentTransaction详解3.Fr...

  • 2019-03-06进阶作业1.0

    目录: 1、背景 2、分析过程 3、结论 4、参考 1、背景 信息分析四期CH1的进阶作业,具体要求如图: 答案:...

  • to be better(个人记录一些点)

    看见别人整理的可研究的专题:【进阶 1 期】 调用堆栈【进阶 2 期】 作用域闭包【进阶 3 期】 this 全面...

网友评论

      本文标题:进阶3作业

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