zoukankan      html  css  js  c++  java
  • ES6之路第五篇:函数的扩展

    属性的简洁表示法

    ES6 允许直接写入变量和函数,作为对象的属性和方法。这样的书写更加简洁。

    1 const foo = 'bar';
    2 const baz = {foo};
    3 baz // {foo: "bar"}
    4 
    5 // 等同于
    6 const baz = {foo: foo};

    上面代码表明,ES6 允许在对象之中,直接写变量。这时,属性名为变量名, 属性值为变量的值。下面是另一个例子。

     1 function f(x, y) {
     2   return {x, y};
     3 }
     4 
     5 // 等同于
     6 
     7 function f(x, y) {
     8   return {x: x, y: y};
     9 }
    10 
    11 f(1, 2) // Object {x: 1, y: 2}

    除了属性简写,方法也可以简写。

     1 const o = {
     2   method() {
     3     return "Hello!";
     4   }
     5 };
     6 
     7 // 等同于
     8 
     9 const o = {
    10   method: function() {
    11     return "Hello!";
    12   }
    13 };

    下面是一个实际的例子。

     1 let birth = '2000/01/01';
     2 
     3 const Person = {
     4 
     5   name: '张三',
     6 
     7   //等同于birth: birth
     8   birth,
     9 
    10   // 等同于hello: function ()...
    11   hello() { console.log('我的名字是', this.name); }
    12 
    13 };

    这种写法用于函数的返回值,将会非常方便。

    1 function getPoint() {
    2   const x = 1;
    3   const y = 10;
    4   return {x, y};
    5 }
    6 
    7 getPoint()
    8 // {x:1, y:10}

    CommonJS 模块输出一组变量,就非常合适使用简洁写法。

     1 let ms = {};
     2 
     3 function getItem (key) {
     4   return key in ms ? ms[key] : null;
     5 }
     6 
     7 function setItem (key, value) {
     8   ms[key] = value;
     9 }
    10 
    11 function clear () {
    12   ms = {};
    13 }
    14 
    15 module.exports = { getItem, setItem, clear };
    16 // 等同于
    17 module.exports = {
    18   getItem: getItem,
    19   setItem: setItem,
    20   clear: clear
    21 };

    属性的赋值器(setter)和取值器(getter),事实上也是采用这种写法。

     1 const cart = {
     2   _wheels: 4,
     3 
     4   get wheels () {
     5     return this._wheels;
     6   },
     7 
     8   set wheels (value) {
     9     if (value < this._wheels) {
    10       throw new Error('数值太小了!');
    11     }
    12     this._wheels = value;
    13   }
    14 }

    注意,简洁写法的属性名总是字符串,这会导致一些看上去比较奇怪的结果。

    1 const obj = {
    2   class () {}
    3 };
    4 
    5 // 等同于
    6 
    7 var obj = {
    8   'class': function() {}
    9 };

    属性名表达式

    ES6 允许字面量定义对象时,用方法二(表达式)作为对象的属性名,即把表达式放在方括号内。

    1 let propKey = 'foo';
    2 
    3 let obj = {
    4   [propKey]: true,
    5   ['a' + 'bc']: 123
    6 };

    下面是另一个例子

     1 let lastWord = 'last word';
     2 
     3 const a = {
     4   'first word': 'hello',
     5   [lastWord]: 'world'
     6 };
     7 
     8 a['first word'] // "hello"
     9 a[lastWord] // "world"
    10 a['last word'] // "world"

    表达式还可以用于定义方法名。

    1 let obj = {
    2   ['h' + 'ello']() {
    3     return 'hi';
    4   }
    5 };
    6 
    7 obj.hello() // hi

    注意,属性名表达式与简洁表示法,不能同时使用,会报错。

    1 // 报错
    2 const foo = 'bar';
    3 const bar = 'abc';
    4 const baz = { [foo] };
    5 
    6 // 正确
    7 const foo = 'bar';
    8 const baz = { [foo]: 'abc'};

    注意,属性名表达式如果是一个对象,默认情况下会自动将对象转为字符串[object Object],这一点要特别小心。

    1 const keyA = {a: 1};
    2 const keyB = {b: 2};
    3 
    4 const myObject = {
    5   [keyA]: 'valueA',
    6   [keyB]: 'valueB'
    7 };
    8 
    9 myObject // Object {[object Object]: "valueB"}

    上面代码中,[keyA][keyB]得到的都是[object Object],所以[keyB]会把[keyA]覆盖掉,而myObject最后只有一个[object Object]属性。

    方法的name属性

     函数的name属性,返回该函数的函数名。

    1 function foo() {}
    2 foo.name // "foo"

    需要注意的是,ES6 对这个属性的行为做出了一些修改。如果将一个匿名函数赋值给一个变量,ES5 的name属性,会返回空字符串,而 ES6 的name属性会返回实际的函数名。

    1 var f = function () {};
    2 
    3 // ES5
    4 f.name // ""
    5 
    6 // ES6
    7 f.name // "f"

    如果将一个具名函数赋值给一个变量,则 ES5 和 ES6 的name属性都返回这个具名函数原本的名字。

    1 const bar = function baz() {};
    2 
    3 // ES5
    4 bar.name // "baz"
    5 
    6 // ES6
    7 bar.name // "baz"

    Function构造函数返回的函数实例,name属性的值为anonymous

    1 (new Function).name // "anonymous"

    bind返回的函数,name属性值会加上bound前缀。

    1 function foo() {};
    2 foo.bind({}).name // "bound foo"
    3 
    4 (function(){}).bind({}).name // "bound "

    箭头函数

    ES6 允许使用“箭头”(=>)定义函数。

    1 var f = v => v;
    2 
    3 // 等同于
    4 var f = function (v) {
    5   return v;
    6 };

    如果箭头函数不需要参数或需要多个参数,就使用一个圆括号代表参数部分。

    1 var f = () => 5;
    2 // 等同于
    3 var f = function () { return 5 };
    4 
    5 var sum = (num1, num2) => num1 + num2;
    6 // 等同于
    7 var sum = function(num1, num2) {
    8   return num1 + num2;
    9 };

    如果箭头函数的代码块部分多于一条语句,就要使用大括号将它们括起来,并且使用return语句返回。

    1 var sum = (num1, num2) => { return num1 + num2; }

    由于大括号被解释为代码块,所以如果箭头函数直接返回一个对象,必须在对象外面加上括号,否则会报错。

    1 // 报错
    2 let getTempItem = id => { id: id, name: "Temp" };
    3 
    4 // 不报错
    5 let getTempItem = id => ({ id: id, name: "Temp" });

    下面是一种特殊情况,虽然可以运行,但会得到错误的结果

    1 let foo = () => { a: 1 };
    2 foo() // undefined

    上面代码中,原始意图是返回一个对象{ a: 1 },但是由于引擎认为大括号是代码块,所以执行了一行语句a: 1。这时,a可以被解释为语句的标签,因此实际执行的语句是1;,然后函数就结束了,没有返回值。

    如果箭头函数只有一行语句,且不需要返回值,可以采用下面的写法,就不用写大括号了。

    1 let fn = () => void doesNotReturn();

    箭头函数可以与变量解构结合使用。

    1 const full = ({ first, last }) => first + ' ' + last;
    2 
    3 // 等同于
    4 function full(person) {
    5   return person.first + ' ' + person.last;
    6 }

    箭头函数使得表达更加简洁。

    1 const isEven = n => n % 2 == 0;
    2 const square = n => n * n;

    上面代码只用了两行,就定义了两个简单的工具函数。如果不用箭头函数,可能就要占用多行,而且还不如现在这样写醒目。

    箭头函数的一个用处是简化回调函数。

    1 // 正常函数写法
    2 [1,2,3].map(function (x) {
    3   return x * x;
    4 });
    5 
    6 // 箭头函数写法
    7 [1,2,3].map(x => x * x);

    另一个例子是

    1 // 正常函数写法
    2 var result = values.sort(function (a, b) {
    3   return a - b;
    4 });
    5 
    6 // 箭头函数写法
    7 var result = values.sort((a, b) => a - b);

    下面是 rest 参数与箭头函数结合的例子。

    1 const numbers = (...nums) => nums;
    2 
    3 numbers(1, 2, 3, 4, 5)
    4 // [1,2,3,4,5]
    5 
    6 const headAndTail = (head, ...tail) => [head, tail];
    7 
    8 headAndTail(1, 2, 3, 4, 5)
    9 // [1,[2,3,4,5]]

    箭头函数的使用注意点

    (1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。

    (2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。

    (3)不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用 rest 参数代替。

    (4)不可以使用yield命令,因此箭头函数不能用作 Generator 函数。

    上面四点中,第一点尤其值得注意。this对象的指向是可变的,但是在箭头函数中,它是固定的。

     1 function foo() {
     2   setTimeout(() => {
     3     console.log('id:', this.id);
     4   }, 100);
     5 }
     6 
     7 var id = 21;
     8 
     9 foo.call({ id: 42 });
    10 // id: 42

    上面代码中,setTimeout的参数是一个箭头函数,这个箭头函数的定义生效是在foo函数生成时,而它的真正执行要等到 100 毫秒后。如果是普通函数,执行时this应该指向全局对象window,这时应该输出21。但是,箭头函数导致this总是指向函数定义生效时所在的对象(本例是{id: 42}),所以输出的是42

    箭头函数可以让setTimeout里面的this,绑定定义时所在的作用域,而不是指向运行时所在的作用域。下面是另一个例子。

     1 function Timer() {
     2   this.s1 = 0;
     3   this.s2 = 0;
     4   // 箭头函数
     5   setInterval(() => this.s1++, 1000);
     6   // 普通函数
     7   setInterval(function () {
     8     this.s2++;
     9   }, 1000);
    10 }
    11 
    12 var timer = new Timer();
    13 
    14 setTimeout(() => console.log('s1: ', timer.s1), 3100);
    15 setTimeout(() => console.log('s2: ', timer.s2), 3100);
    16 // s1: 3
    17 // s2: 0

    上面代码中,Timer函数内部设置了两个定时器,分别使用了箭头函数和普通函数。前者的this绑定定义时所在的作用域(即Timer函数),后者的this指向运行时所在的作用域(即全局对象)。所以,3100 毫秒之后,timer.s1被更新了 3 次,而timer.s2一次都没更新。

    箭头函数可以让this指向固定化,这种特性很有利于封装回调函数。下面是一个例子,DOM 事件的回调函数封装在一个对象里面。

     1 var handler = {
     2   id: '123456',
     3 
     4   init: function() {
     5     document.addEventListener('click',
     6       event => this.doSomething(event.type), false);
     7   },
     8 
     9   doSomething: function(type) {
    10     console.log('Handling ' + type  + ' for ' + this.id);
    11   }
    12 };

    上面代码的init方法中,使用了箭头函数,这导致这个箭头函数里面的this,总是指向handler对象。否则,回调函数运行时,this.doSomething这一行会报错,因为此时this指向document对象。

    this指向的固定化,并不是因为箭头函数内部有绑定this的机制,实际原因是箭头函数根本没有自己的this,导致内部的this就是外层代码块的this。正是因为它没有this,所以也就不能用作构造函数。

     1 // ES6
     2 function foo() {
     3   setTimeout(() => {
     4     console.log('id:', this.id);
     5   }, 100);
     6 }
     7 
     8 // ES5
     9 function foo() {
    10   var _this = this;
    11 
    12   setTimeout(function () {
    13     console.log('id:', _this.id);
    14   }, 100);
    15 }

    上面代码中,转换后的 ES5 版本清楚地说明了,箭头函数里面根本没有自己的this,而是引用外层的this

    请问下面的代码之中有几个this

     1 function foo() {
     2   return () => {
     3     return () => {
     4       return () => {
     5         console.log('id:', this.id);
     6       };
     7     };
     8   };
     9 }
    10 
    11 var f = foo.call({id: 1});
    12 
    13 var t1 = f.call({id: 2})()(); // id: 1
    14 var t2 = f().call({id: 3})(); // id: 1
    15 var t3 = f()().call({id: 4}); // id: 1

    上面代码之中,只有一个this,就是函数foothis,所以t1t2t3都输出同样的结果。因为所有的内层函数都是箭头函数,都没有自己的this,它们的this其实都是最外层foo函数的this

    除了this,以下三个变量在箭头函数之中也是不存在的,指向外层函数的对应变量:argumentssupernew.target

    1 function foo() {
    2   setTimeout(() => {
    3     console.log('args:', arguments);
    4   }, 100);
    5 }
    6 
    7 foo(2, 4, 6, 8)
    8 // args: [2, 4, 6, 8]

    上面代码中,箭头函数内部的变量arguments,其实是函数fooarguments变量。

    另外,由于箭头函数没有自己的this,所以当然也就不能用call()apply()bind()这些方法去改变this的指向。

    1 (function() {
    2   return [
    3     (() => this.x).bind({ x: 'inner' })()
    4   ];
    5 }).call({ x: 'outer' });
    6 // ['outer']

    上面代码中,箭头函数没有自己的this,所以bind方法无效,内部的this指向外部的this

    长期以来,JavaScript 语言的this对象一直是一个令人头痛的问题,在对象方法中使用this,必须非常小心。箭头函数”绑定”this,很大程度上解决了这个困扰。

    嵌套的箭头函数

    箭头函数内部,还可以再使用箭头函数。下面是一个 ES5 语法的多重嵌套函数。

     1 function insert(value) {
     2   return {into: function (array) {
     3     return {after: function (afterValue) {
     4       array.splice(array.indexOf(afterValue) + 1, 0, value);
     5       return array;
     6     }};
     7   }};
     8 }
     9 
    10 insert(2).into([1, 3]).after(1); //[1, 2, 3]

    上面这个函数,可以使用箭头函数改写。

    1 let insert = (value) => ({into: (array) => ({after: (afterValue) => {
    2   array.splice(array.indexOf(afterValue) + 1, 0, value);
    3   return array;
    4 }})});
    5 
    6 insert(2).into([1, 3]).after(1); //[1, 2, 3]

    双冒号运算符

    箭头函数可以绑定this对象,大大减少了显式绑定this对象的写法(callapplybind)。但是,箭头函数并不适用于所有场合,所以现在有一个提案,提出了“函数绑定”(function bind)运算符,用来取代callapplybind调用。

    函数绑定运算符是并排的两个冒号(::),双冒号左边是一个对象,右边是一个函数。该运算符会自动将左边的对象,作为上下文环境(即this对象),绑定到右边的函数上面。

     1 foo::bar;
     2 // 等同于
     3 bar.bind(foo);
     4 
     5 foo::bar(...arguments);
     6 // 等同于
     7 bar.apply(foo, arguments);
     8 
     9 const hasOwnProperty = Object.prototype.hasOwnProperty;
    10 function hasOwn(obj, key) {
    11   return obj::hasOwnProperty(key);
    12 }

    如果双冒号左边为空,右边是一个对象的方法,则等于将该方法绑定在该对象上面。

    1 var method = obj::obj.foo;
    2 // 等同于
    3 var method = ::obj.foo;
    4 
    5 let log = ::console.log;
    6 // 等同于
    7 var log = console.log.bind(console);

    如果双冒号运算符的运算结果,还是一个对象,就可以采用链式写法。

    1 import { map, takeWhile, forEach } from "iterlib";
    2 
    3 getPlayers()
    4 ::map(x => x.character())
    5 ::takeWhile(x => x.strength > 100)
    6 ::forEach(x => console.log(x));

    尾调用优化

    1.什么是尾调用

    尾调用(Tail Call)是函数式编程的一个重要概念,本身非常简单,一句话就能说清楚,就是指某个函数的最后一步是调用另一个函数。

    1 function f(x){
    2   return g(x);
    3 }

    上面代码中,函数f的最后一步是调用函数g,这就叫尾调用。

    以下三种情况,都不属于尾调用

     1 // 情况一
     2 function f(x){
     3   let y = g(x);
     4   return y;
     5 }
     6 
     7 // 情况二
     8 function f(x){
     9   return g(x) + 1;
    10 }
    11 
    12 // 情况三
    13 function f(x){
    14   g(x);
    15 }

    上面代码中,情况一是调用函数g之后,还有赋值操作,所以不属于尾调用,即使语义完全一样。情况二也属于调用后还有操作,即使写在一行内。情况三等同于下面的代码。

    1 function f(x){
    2   g(x);
    3   return undefined;
    4 }

    尾调用不一定出现在函数尾部,只要是最后一步操作即可。

    1 function f(x) {
    2   if (x > 0) {
    3     return m(x)
    4   }
    5   return n(x);
    6 }

    上面代码中,函数mn都属于尾调用,因为它们都是函数f的最后一步操作。

    2.尾调用优化

    尾调用由于是函数的最后一步操作,所以不需要保留外层函数的调用帧,因为调用位置、内部变量等信息都不会再用到了,只要直接用内层函数的调用帧,取代外层函数的调用帧就可以了。

     1 function f() {
     2   let m = 1;
     3   let n = 2;
     4   return g(m + n);
     5 }
     6 f();
     7 
     8 // 等同于
     9 function f() {
    10   return g(3);
    11 }
    12 f();
    13 
    14 // 等同于
    15 g(3);

    上面代码中,如果函数g不是尾调用,函数f就需要保存内部变量mn的值、g的调用位置等信息。但由于调用g之后,函数f就结束了,所以执行到最后一步,完全可以删除f(x)的调用帧,只保留g(3)的调用帧。

    这就叫做“尾调用优化”(Tail call optimization),即只保留内层函数的调用帧。如果所有函数都是尾调用,那么完全可以做到每次执行时,调用帧只有一项,这将大大节省内存。这就是“尾调用优化”的意义。

    3.尾递归

    函数调用自身,称为递归。如果尾调用自身,就称为尾递归。

    递归非常耗费内存,因为需要同时保存成千上百个调用帧,很容易发生“栈溢出”错误(stack overflow)。但对于尾递归来说,由于只存在一个调用帧,所以永远不会发生“栈溢出”错误。

    1 function factorial(n) {
    2   if (n === 1) return 1;
    3   return n * factorial(n - 1);
    4 }
    5 
    6 factorial(5) // 120

    上面代码是一个阶乘函数,计算n的阶乘,最多需要保存n个调用记录,复杂度 O(n) 

    如果改写成尾递归,只保留一个调用记录,复杂度 O(1) 。

    1 function factorial(n, total) {
    2   if (n === 1) return total;
    3   return factorial(n - 1, n * total);
    4 }
    5 
    6 factorial(5, 1) // 120

    4.递归函数的改写

    尾递归的实现,往往需要改写递归函数,确保最后一步只调用自身。做到这一点的方法,就是把所有用到的内部变量改写成函数的参数。比如上面的例子,阶乘函数 factorial 需要用到一个中间变量total,那就把这个中间变量改写成函数的参数。这样做的缺点就是不太直观,第一眼很难看出来,为什么计算5的阶乘,需要传入两个参数51

     1 function tailFactorial(n, total) {
     2   if (n === 1) return total;
     3   return tailFactorial(n - 1, n * total);
     4 }
     5 
     6 function factorial(n) {
     7   return tailFactorial(n, 1);
     8 }
     9 
    10 factorial(5) // 120
  • 相关阅读:
    MongoDB的基础连接
    Pygame的图像移动
    Pygame的KEYDOWN和KEYUP
    Pygame的程序开始
    Tkinter的MessageBox组件
    流程控制语句-if...else语句
    流程控制语句-if判断
    分享数组概念、创建、赋值、长度与使用
    js入门与实战课前准备和编辑工具介绍
    JavaScript进阶篇-JS基础语法
  • 原文地址:https://www.cnblogs.com/wanghao123/p/9299443.html
Copyright © 2011-2022 走看看