liushuangls / memo

用于记录
1 stars 0 forks source link

JavaScript函数与作用域 #12

Closed liushuangls closed 6 years ago

liushuangls commented 7 years ago

函数声明和函数表达式的区别

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

arguments 是什么

函数的"重载"怎样实现

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

求n!,用递归来实现

  function factorial(n) {
    if (n === 1) {
      return 1
    }
    return n * factorial(n - 1)
  }
  factorial(5) // 120

以下代码输出什么?

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; 
// ["小谷",3,....];
// name valley

getInfo('男');
// name: 男; age: undefined; sex: undefined; 
// ["男",....];
// name valley

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

function sumOfSquares(){
  var arr = Array.from(arguments)
  var result = 0
  arr.forEach(function (x) {
     result += x * x
  })
  return result
}
var result = sumOfSquares(2,3,4)
var result2 = sumOfSquares(1,3)
console.log(result)  //29
console.log(result2)  //10

如下代码的输出?为什么

console.log(a);
// undifined, 虽然会发生变量提升,但是a还没有赋值
var a = 1;
console.log(b);
// 报错,访问一个不存在的变量

如下代码的输出?为什么

sayName('world');
// hello world,函数声明提升
sayAge(10);
// 报错,函数表达式,在执行到赋值语句之前相当于声明一个变量,所以sayAge的值此时为undifned

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

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

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.
fooContext = {
  AO: {}
  Scope: globalContext.AO
}
3.
barContext = {
  AO: {
    x: 30
  }
  Scope: globalContext.AO
}

// 输出10,首先执行bar,声明一个局部变量x,然后执行foo,foo输出x,因为foo的
    活动对象中找不到x,所以向foo的作用域链上端查找,也就是globalContext,然后找到x为10

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

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.
barContext = {
  AO: {
    x: 30
     : function
  }
  Scope: globalContext.AO
}
 .[[scope]] = barContext.AO
// 输出30,立即执行函数会先在自己的作用域内寻找,然后进入bar的作用域中找到x=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)

伪代码:
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: globalContext
}
fn2.[[scope]] = fnContext.AO
3.
fn3Context = {
  AO: {}
  Scope: globalContet.AO
}
4.
fn2Context = {
  AO: {}
  Scope: fnContext
}

// 输出:undifined, 5, 1, 6, 20, 200
// 解析:执行fn,进入它的作用域,变量提升,所以第一个输出undefined,第二个a赋值后输出5,然后a+1;
接着执行fn3,在它的作用域中找不到a,进入全局作用域,输出a=1,将全局a赋值为200;
接着执行fn2,在它的作用域中找不到a,进入fn作用域,输出a=6,将fn作用域中的a赋值为20;
接着输出fn中的a=20;
接着输出全局的a=200;