美文网首页
无标题文章

无标题文章

作者: 饥人谷_林嘉俊 | 来源:发表于2017-08-22 21:58 被阅读6次

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

函数声明的形式 function fn(){ } 调用 fn() ;
函数表达式的形式 var XX = function fn(){ } 调用 XX();
区别: 形式不同外,调用方式不同外
函数声明提前,函数声明的位置无论在哪里,调用方法都可以使用,因为函数已经提前定义了
函数表达式则要先定义函数表达式,再调用

    // 函数表达式
    // 1 前后两个函数的名字可以相同也可以不相同
    // 2 function 后面的这个名字是可以省略的 
    // 3 function 后面的这个名字只能再函数内部使用
    // 4 函数调用只有在函数表达式声明后调用。
    // 函数声明
    // function fn() {}
    // fn();
    //1 函数声明必须有函数名
    //2 可以在任意地方调用fn();

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

什么是变量的声明前置?

JavaScript引擎的工作方式是,先解析代码,获取所有被声明的变量,然后再一行一行地运行。这造成的结果,就是所有的变量的声明语句,都会被提升到代码的头部,然后给他初始值undefined,然后才逐句执行程序,这就叫做“变量提升”,也即“变量的声明前置”。

什么是函数的声明前置?

和变量的声明会前置一样,函数声明同样会前置,如果我们使用函数表达式那么规则和变量一样,如下图:

如果我们使用函数声明的方式,那么即使函数写在最后也可以在前面语句调用.

arguments 是什么

arguments 是个类数组,能使用arguments[i]和arguments.length
arguments 里面是函数的具体参数,保存的是实际传入的参数,而不局限于函数声明所定义的参数列表
https://baike.baidu.com/item/arguments/4291417?fr=aladdin

函数的"重载"怎样实现

其它面向对象语言如Java的一个常见特性是:能够根据传入的不同数量量或类型的参数,通过“重载”函数来发挥不同的功用。但是这个特性在Javascript中并没有被直接支持,可是有很多办法实现这一功能。
不管何种面向对象语,函数重载必须依赖两件事情,分别是:
1)判断传入参数数量的能力
2)判断传入参数类型的能力

函数的argument是包含传入参数的类数组,它有数组的两个特性,一是可以使用数组的length方法,二是可以通过argument[index]获得具体的参数值
所以可以通过argument 来实现
举例:

function send( message, who ) {
if( arguments.length >= 2 ) {
 console.log("你对" + who + "说:" + message);
 } else {
console.log("你对大家说:" + message);}}

这个例子是判断传入参数的数值来实现重载

例2

  function send( message ) {
 if( typeof message === 'undefined' ) {
 console.log("ERROR:错误的信息内容");
  } else { console.log("你说:" + message); }}

这个例子是判断参数传入的类型来实现重载

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

立即执行函数表达式有多种写法:(function(){})();, 或(function(){}()); ,或!function(){}();,或void function(){}();

作用:创建一个独立的作用域。这个作用域里面的变量,外面访问不到(即避免变量污染)。

求n!,用递归来实现

function count(n){
  if(n < 0){
      return "wrong";
 }else if(n ===0 || n === 1){
      return 1;
  }else{
      return n*count(n-1);
}
}

以下代码输出什么?

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
undefined

image.png

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

 function sumOfSquares(){
        var result = 0;
        for(var i=0;i<arguments.length;i++){
              result += arguments[i]*arguments[i];
        }
        return result;
   }
   var result = sumOfSquares(2,3,4)
   var result2 = sumOfSquares(1,3)
   console.log(result)  //29
   console.log(result2)  //10

如下代码的输出?为什么

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

首先会输出 undefined
其次 Uncaught ReferenceError: b is not defined
因为 变量声明前置,变量提前声明,声明的值为undefined
b 没有定义,所以会报错

如下代码的输出?为什么

sayName('world');
sayAge(10);
function sayName(name){
    console.log('hello ', name);
}
var sayAge = function(age){
    console.log(age);
};

输出结果为
hello world
Uncaught TypeError: sayAge is not a function
原因 函数声明前置,函数表达式不会,函数表达式要先声明再用,所以sayName('world')可以执行

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

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

10


image.png

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

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

30

image.png

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

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

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

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)

undefined
5
1
6
20
200


image.png

相关文章

  • 无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章 无标题文章无标题文章无标题文章无...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • fasfsdfdf

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章 无标题文章 无标题文章无标题文章 无标题文章 无标题文章

网友评论

      本文标题:无标题文章

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