JavaScript 函数的scope,currying

函数的scope

当定义一个函数时,同时创建了一个scope。
函数内定义的任何东西都不能在函数外访问,只有函数内可以访问。

function foo() {
  var a = 'hello';
  console.log(a);  // => 'hello'
}

console.log(a);  // reference error

内嵌函数也使用同样的规则。

function foo() {
  var a = 'hello';

  function bar() {
    var b = 'world';
    console.log(a);  // => 'hello'
    console.log(b);  // => 'world'
  }

  console.log(a);  // => 'hello'
  console.log(b);  // reference error
}

console.log(a);  // reference error
console.log(b);  // reference error

JavaScript首先从当前scope查找引用,如果找不到,就向外围找,直到找到为止,找不到就报错。

var a = 'hello';

function foo() {
  var b = 'world';

  function bar() {
    var c = '!!';

    console.log(a);  // => 'hello'
    console.log(b);  // => 'world'
    console.log(c);  // => '!!'
    console.log(d);  // reference error
  }
}

这也意味着同名的变量,只看内部的不看外部的。

var a = 'hello';

function foo() {
  var a = 'world';

  function bar() {
    console.log(a);  // => 'world'
  }
}

这规则同样适用于const 关键字,用 const 声明一个变量表示你不能改变这个变量的值。但是在函数中声明就可以创建一个新的scope了,进而赋新值。

function foo() {
  const a = true;

  function bar() {
    const a = false;  // different variable
    console.log(a);   // false
  }

  const a = false;    // SyntaxError
  a = false;          // TypeError
  console.log(a);     // true
}

Currying

currying 是有n个入参的函数的变形,把n个入参变成只有一个入参的n个函数序列。

使用场景是这样的:当某些入参的值对于后面的入参有用时,你可以用currying把一个函数分解成一系列函数,逐步完成这项任务。这样的好处是:

  • 当一个入参的值几乎不变时,但你需要维持那个值的灵活性(而不是写死成常量)。
  • 当一个currying函数的结果对于后面的currying函数运行前有用时。
  • 以特定顺序验证多个函数的返回值。

比如,立方体的体积是3个因子:长宽高:

var prism = function(l, w, h) {
    return l * w * h;
}

变成currying就是:

function prism(l) {
    return function(w) {
        return function(h) {
            return l * w * h;
        }
    }
}

ES6以后可以这样写:

var prism = l => w => h => l * w * h;

你可以像这样调用:

var prism = l => w => h => l * w * h;

立即被调用的函数表达式

有时你不想让函数作为一个变量被调用,可以创建一个立即被调用的函数表达式,也叫做IIFE(Immediately Invoked Function Expression)。它本质上是自执行匿名函数(self-executing anonymous functions)。它们可以访问周围的scope,但是函数本身及其内部变更不能被外部访问。一个重要点需要记住:如果你的函数不是匿名的,IIFE就不会像标准函数那样hoist,并且不能通过其声明的名称调用。

Hoist 的机制是:移动所有变量和函数的声明到它的scope的最顶部。但是,赋值还在声明的地方发生。

(function() {
   alert("I've run - but can't be run again because I'm immediately invoked at runtime,
          leaving behind only the result I generate");
}());

这是另一种写法,注意分号前的括号移到了花括号之后。

(function() {
   alert("This is IIFE too.");
})();

传参:

(function(message) {
   alert(message);
}("Hello World!"));

另外,你可以给外围scope返回值:

var example = (function() {
   return 42;
}());
console.log(example); // => 42

如果有需要的话,可以给IIFE全名,但不常见,这种模式有几个优点,比如提供一个引用,可以用于递归,并且在调试时也更容易,因为在callstack中有名称。

(function namedIIFE() { 
    throw error; // We can now see the error thrown in 'namedIIFE()'
}());

把函数放进小括号是常用的方式。

var a = function() { return 42 }();
console.log(a)  // => 42

箭头函数版本的IIFE:

(() => console.log("Hello!"))(); // => Hello!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值