函数与作用域

2017-12-19  本文已影响0人  Tuuu

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

// 函数声明
function sayHello(){
    console.log('hello')
}

//函数调用
sayHello();
// 函数表达式
var sayHello = function(){
    console.log('hello');
}

// 函数调用
sayHello()

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

arguments 是什么

function printInfo(name, age, sex){
    console.log(name);
    console.log(age);
    console.log(sex);
    console.log(arguments);
    console.log(arguments[0]); 
}

函数的"重载"怎样实现

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

printInfo('Tuuu', 20);
printInfo('Tuuu', 20, 'male');

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

!function (){
    var a;
    a = 1;
    console.log(1);
}()
// ! 可以更换为 +/-/~/() 来使用,其中 () 后面必须加分号。

目的有两个:一是不必为函数命名,避免了污染全局变量;二是为了得到一个独立的作用域,可以封装一些外部无法读取的私有变量。

求n!,用递归来实现

代码如下

function xxx(num) {
  if (num < 2) {
    return 1;
  }
  else{
    return (num*(xxx(num - 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, '男');  
name: 饥人谷
age: 2
sex: 男
Arguments(3) ["饥人谷", 2, "男"]
name valley

getInfo('小谷', 3);
name: 小谷
age: 3
sex: undefined
Arguments(2) ["小谷", 3]
name valley

getInfo('男');
name: 男
age: undefined
sex: undefined
Arguments(1) ["男"]
name valley

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

 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;
  }

如下代码的输出?为什么

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

输出结果:

undefined, 变量提升将 var a 被提升到作用域最上方,输出 a 时 a 还没有被赋值
报错,b is not defined, 因为b 没有被声明

如下代码的输出?为什么

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

变量声明前置后

var sayAge
function sayName(name){
    console.log('hello ', name);
}
sayName('world'); // hello, world
sayAge(10);  // 报错 sayAge is not a function
sayAge = function(age){
    console.log(age);
}

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

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

foo.[[scope]] = globalContext.AO
bar.[[scope]] = globalContext.AO

fooContext = {
  AO:{}
  scope: foo[[.scope]] 
}

barContext = {
  AO: {
    x = 30;
    foo: function
  }
  scope: bar[[.scope]] 
}
 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;
  }
}

bar.[[scope]] = globalContext.AO

barContext = {
  AO: {
    x = 30;
    foo: function
  }
  scope: bar[[.scope]]
}

foo[[.scope]] 

fooContext = {
  AO:{}
  scope: foo[[.scope]] 
}
console.log(x)  // 30 

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

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

bar.[[scope]] = globalContext.AO

barContext = {
  AO: {
    x = 30;
    function: function
  }
  scope: bar[[.scope]]
}

function[[.scope]] 

functionContext = {
  AO:{}
  scope: function[[.scope]]
}
console.log(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)
globalContext {
  AO: {
    a = 1;
    fn: function
    fn3: function
  }
}

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

fnContext {
  AO: {
    a = undefined
    f3 = function
    f2 = function
  }
  scope: fn[[.scope]] 
}

fn2[[.scope]] = fnContext.AO

fn2Context {
  AO: {}
  scope: fn2[[.scope]] 
}

fn3Context {
  AO: {}
  scope: fn3[[.scope]]
}

// 输出结果: undefined 5 1 6 20 200
上一篇下一篇

猜你喜欢

热点阅读