进阶-3

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

构造函数常用的两种方法:函数声明和函数表达式

     //函数声明
    function sum(a, b) {
        return a + b;
    }
    alert(sum(1, 2));
  •   //函数表达式
      var s = function sum(a, b) {
          return a + b;
      }
      alert(s(1, 2)); 
     /*///////////////////////////////*/
      var s = function(a, b) {
          return a + b;
      }
      alert(s(1, 2));
      //以上两种都可以
    

这二者区别是:

可以看到函数声明必须有函数名,而函数表达式中的函数名可以忽略

由于JavaScript解析器对这两种定义方式读取的顺序不同所以用函数声明定义的函数,函数可以在函数声明之前调用,而用函数表达式定义的函数只能在声明之后调用。


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

变量声明前置是将变量的声明提到当前作用域的开头,比如:

console.log(a);    //undefined
var a = 1;
console.log(a);    //   1
//  上面的代码在函数解析的过程是这样的
var  a;
console.log(a);      // undefined
a = 1 ;
console.log(a);    //    1

变量声明前置只是将其提前声明,对于它的赋值则还在原来的位置。

函数的声明前置,举个栗子:

fun1();      //  123
function fun1(){
console.log(123);   
}

函数声明前置是将函数声明提前到当前作用域开头,注意函数声明前置比变量声明前置优先级更高,可以在当前作用域下任何地方调配此函数,区别于函数表达式所必需遵守的先后顺序。


3.arguments 是什么

arguments是一个类似数组的对象, 对应于传递给函数的参数。
arguments 对象仅在函数内部有效,在函数外部调用 arguments 对象会出现一个错误。
可以使用arguments对象在函数中引用函数的参数。此对象包含传递给函数的每个参数的条目,比如:

function Resume(name,age,sex){
console.log(arguments[0]);   //   等效于console.log(name);
console.log(arguments[1]);   //         console.log(age);
console.log(arguments[2]);   //         console.log(sex);
}
Resume('XiaoMing',21,'male');

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

函数重载是指:形参不同的多个同名函数根据处理数据的不同而返回不同的结果。
函数重载主要运用于c++,java等强类型语言中,因为JavaScript是若类型语言,构建同名函数会把之前的覆盖掉,因此在JS中没用重载,但是可以运用一些技巧达到重载的效果。比如:

function fun1(obj) { alert(1) }
function fun1(obj, obj1, obj2) { alert(3) }
function fun1(obj2,obj3) {alert(2) }
fun1();

这样的代码在JS中只会弹出“2”,因为后面的函数会覆盖掉前面的同名函数,那要怎么样才能打到想要的效果呢,这就需要添加判定条件了

function fun1(obj) { alert(1) }
function fun3(obj, obj1, obj2) { alert(3) }
function fun2(obj, obj1) { alert(2) }


function funAll(obj, obj1, obj2, obj3) {
    if ( arguments.length == 1) {
        fun1(obj);
    }
    else if ( arguments.length == 2) {
        fun2(obj, obj1);
    }
    else if ( arguments.length == 3) {
        fun3(obj, obj1, obj2);
    }
}

funAll("");
funAll("", "");
funAll("", "","");

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

立即执行函数模式是一种语法,可以让你的函数在定义后立即被执行,作用是隔离作用域,独立起来不影响全局

(function () { 
alert('hello world');
})()

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

function factor(n){
  if(n < 0){
      return false;
  }else if(n <= 1){
      return 1;
  }else{
      return n * factor(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, '男'); //name:饥人谷 age:2 sex:男 ['饥人谷', 2, '男'] name valley
getInfo('小谷', 3); //name:小谷 age:3 sex:undefined ['饥人谷', 2, '男'] name valley
getInfo('男'); //name:男 age:undefined sex:undefined ['饥人谷', 2, '男'] name valley

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

function sumOfSquares(a,b,c){
  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    变量a声明前置,但是还未赋值
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()                    //10
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.barContext = {
    AO: {
        x: 30
    }
    Scope: bar.[[scope]] = globalContext.AO;
}

3.fooContext = {
    AO: {}
    Scope: foo.[[scope]] = globalContext.AO;
}  */

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

var x = 10;
bar()             // 30
function bar(){
  var x = 30;
  function foo(){
  console.log(x) 
  }
  foo();
}

//////////////////////////////////////////////////

/* 1.globalContext = {
      AO:{
            X: 10
            bar: function(){}
    }
    scopr: null
}
bar.[[scope]] = globalContext.AO

    2.barContext = {
      AO: {
        x: 30
        foo: function (){}
    }
    Scope: bar.[[scope]] = globalContext.AO
}

foo.[[scope]] = barContext.AO

    3.fooContext = {
      AO: {}
    Scope: foo.[[scope]] = barContext.AO
} */

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

var x = 10;
bar()        //30
function bar(){
  var x = 30;
  (function (){
    console.log(x)
  })()
}

/////////////////////////////////////////////////////////

/* 1.globalContext = {
      AO: {
        x: 10
        bar: function (){}
    }
    Scope: null
}

bar.[[scope]] = globalContext.AO

  2.barContext = {
      AO: {
        x: 30
        IIFE
    }
    Scope: bar.[[scope]] = globalContext.AO
}
IIFE.[[scope]] = barContext.AO

  3.IIFEContext {
      AO: {}
    Scope: IIFE.[[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()
console.log(a)     //200

//////////////////////////////////////////////////////

/*1.globalContext = {
      AO: {
        a: 1
        fn: function(){}
        fn3: function(){}
    }
    Scope: null
}

fn.[[scope]] = globalContext.AO
fn3.[[scope]] = globalContext.AO

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

fn2.[[scope]] = fnContext.AO

  3.fn3Context = {
      AO: {}
    Scope: fn3.[[scope]] = globalContext.AO
}

  4.fn2Context = {
      AO: {}
    Scope: fnContext.AO
}*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.函数声明和函数表达式有什么区别 函数声明 代码执行时函数声明会被提升到最前执行,所以函数的调用与函数声明的顺序...
    Feiyu_有猫病阅读 3,038评论 0 0
  • 1.函数声明和函数表达式有什么区别 使用函数声明来声明的函数不需要放在调用的前面就可以被调用,使用函数表达式声明的...
    饥人谷_星璇阅读 1,571评论 0 0
  • 函数声明和函数表达式有什么区别 使用函数声明,如果函数被提前调用也是可以正常运行的;如果使用函数表达式,和变量提升...
    饥人谷_严琰阅读 1,652评论 0 0
  • 1. 函数声明和函数表达式有什么区别 函数声明不必放在调用前面 函数表达式必须放在调用前面 2. 什么是变量的声明...
    饥人谷_js_chen阅读 1,756评论 0 0
  • 这两天的阅读课刚好上到“圣诞颂歌”,就是那个讲了一个超级吝啬的死老头最后幡然醒悟成为好人的故事。 最开始的时候,我...
    疏安安安阅读 5,000评论 0 1

友情链接更多精彩内容