函数与作用域

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

声明函数:ECMAScript规定了三种声明函数方式

  • 构造函数
    首先函数也是对象的一种,我们可以通过其构造函数,使用new来创建一个 函数对象
  var sayHello = new Function("console.log('hello world');");//不推荐使用
  • 函数声明
    使用function关键字可以声明一个函数
 //函数声明
  function sayHello(){
    console.log('hello')
  }

  //函数调用
  sayHello()
//声明不必放到调用的前面
  • 函数表达式
  var sayHello = function(){
    console.log('hello');
  }

  sayHello()
//声明必须放到调用的前面

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

  • 在一个作用域下,var 声明的变量和function 声明的函数会前置
console.log(a); //undefined
var a = 3;
console.log(a); //3

sayHello();

function sayHello(){
  console.log('hello');
}
  • 函数内部的声明前置
function fn(){
  console.log(a)  //undefined
  var a = 3
  console.log(a)
}
fn()

arguments 是什么

函数的"重载"怎样实现

在 JS 中没有重载! 同名函数会覆盖。 但可以在函数体针对不同的参数调用执行相应的逻辑

function printPeopleInfo(name, age, sex){
    if(name){
      console.log(name);
    }

    if(age){
      console.log(age);
    }

    if(sex){
      console.log(sex);
    }
  }


  printPeopleInfo('Byron', 26);
  printPeopleInfo('Byron', 26, 'male');

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

  • 立即执行函数表达式
(function(){
  var a  = 1;
})()
console.log(a); //undefined
  • 作用: 隔离作用域

求n!,用递归来实现

function factor(n){
  if(n === 1) {
    return 1
  }
  return n * factor(n-1)
}

factor(5)

以下代码输出什么?

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: 男
Arguments[3]
//
name valley
name: 小谷
age: 3
 sex: undefined
//
name valley
name: 男
age: undefined
sex: undefined
//
name valley

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

   function sumOfSquares(){
        var sum=0;
             for(var key in arguments){
              sum+=arguments[key]*arguments[key]

    }
   return sum;
   }
   var result = sumOfSquares(2,3,4)
   var result2 = sumOfSquares(1,3)
   console.log(result)  //29
   console.log(result2)  //10

如下代码的输出?为什么

    console.log(a);//undefined
    var a = 1;
    console.log(b);// b is not defined
/*
此代码执行顺序为:
var a -> console.log(a) 结果为 undefined
-> a = 1 -> console.log(b) 报错,"b"  未定义 
*/

如下代码的输出?为什么

    sayName('world');
    sayAge(10);
    function sayName(name){
        console.log('hello ', name);
    }//hello  world
    var sayAge = function(age){
        console.log(age);
    };/*sayAge is not a function:var sayAge 变量声明前置,
但只会声明此变量不会将其赋值为函数,所以函数 sayAge 的调用必须在函数赋值之后,否则会报错。*/

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

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

/*globalContext = {
  AO: {
    x: 10
    foo: function
    bar: function
  },
  Scope: null
}

//声明 foo 时 得到下面
foo.[[scope]] = globalContext.AO
//声明 bar 时 得到下面
bar.[[scope]] = globalContext.AO

当调用 bar() 时, 进入 bar 的执行上下文

barContext = {
  AO: {
    x: 30
  },
  Scope: bar.[[scope]] //globalContext.AO
}
当调用 foo() 时,先从 bar 执行上下文中的 AO里找,找不到再从 bar 的 [[scope]]里找

找到后即调用
当调用 foo() 时,进入 foo 的执行上下文

fooContext = {
  AO: {},
  Scope: foo.[[scope]] // globalContext.AO
}

所以 console.log(x)是 10
*/

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

var x = 10;
bar() 
function bar(){
  var x = 30;
  function foo(){
    console.log(x) 
  }
  foo();
}   
/*globalContext = {
  AO: {
    x: 10
    bar: function
  },
  Scope: null
}

//声明 bar 时 得到下面
bar.[[scope]] = globalContext.AO

注意: 在当前的执行上下文内声明的函数,这个函数的[[scope]]就执行当前执行上下文的 AO

当调用 bar() 时, 进入 bar 的执行上下文

barContext = {
  AO: {
    x: 30,
    foo: function
  },
  Scope: bar.[[scope]] //globalContext.AO
}
//在 bar 的执行上下文里声明 foo 时 得到下面
foo.[[scope]] = barContext.AO
当调用 foo() 时,先从 bar 执行上下文中的 AO里找,找到后即调用

笔试题
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); 

当调用 bar() 时, 进入 bar 的执行上下文

barContext = {
  AO: {
    x: 30,
    foo: function
  },
  Scope: bar.[[scope]] //globalContext.AO
}
//在 bar 的执行上下文里声明 foo 时 得到下面
foo.[[scope]] = barContext.AO

当调用 foo() 时,先从 bar 执行上下文中的 AO里找,找到后即调用

当调用 foo() 时,进入 foo 的执行上下文

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

所以 console.log(x)是 30
*/

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

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

/*
(function (){
    console.log(x)
  })()等同于
var f =function (){
}f();

globalContext = {
AO: {
    x: 10;
    bar: function       
}
bar.[[scope]] = globalContext.AO
}

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

functionContext = {
AO: {},
Scope: barContext.AO
}
barContext.AO{x = 30}
输出30;
*/

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

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)

/*
globalContext = {
AO: {
    a:200;
    fn: function
   fn3 :function      
}

}
fn.[[scope]] = globalContext.AO
fn3.[[scope]] = globalContext.AO
fnContext = {
AO: {
    a: 20
},
scope:globalContext.AO
}
fn2.[[scope]] =fnlContext.AO

fn3Context = {
AO: {
    a: 20
    fn2:function(){}
},
scope:globalContext.AO
}

fn2Context = {
AO: {
    a: 
    fn2:function(){}
},
scope:globalContext.AO
}
//undefined 5 1 6 20 200
*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,045评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,114评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,120评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,902评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,828评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,132评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,590评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,258评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,408评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,335评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,385评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,068评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,660评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,747评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,967评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,406评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,970评论 2 341

推荐阅读更多精彩内容

  • 函数声明和函数表达式有什么区别 函数声明语法:function functionName(arg0,arg1,ar...
    _Dot912阅读 562评论 0 3
  • 1.函数声明和函数表达式有什么区别 函数就是一段可以反复调用的代码块。函数还能接受输入的参数,不同的参数会返回不同...
    徐国军_plus阅读 470评论 0 0
  • 1.函数声明和函数表达式有什么区别 函数声明:使用function关键字声明函数,声明不必放到调用的前面函数声明在...
    BAWScipes阅读 251评论 0 0
  • 1.函数声明和函数表达式有什么区别 函数声明使用function关键字进行声明,声明不必放到调用的前面。因为函数可...
    山门龙龙阅读 312评论 0 0
  • 总是懒于从头梳理回忆,再用一个个文字串联起来。可是这一次无论如何,我想这样做一次。 网投以来,就是一路惊喜不断。 ...
    水来土掩阅读 407评论 0 1