使用参数变量获取传递给 ES6 箭头函数的参数 [英] Getting arguments passed to an ES6 arrow function using arguments variable

查看:36
本文介绍了使用参数变量获取传递给 ES6 箭头函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解箭头函数在 ES6 中的工作原理以及词法 this,但我想知道是否有人知道将参数传递给箭头函数的方法?

I understand how arrow functions work in ES6, and the lexical this, but I was wondering if anyone knows of a way to get the arguments passed to an arrow function?

在 ES5 中,你可以简单地做:

In ES5, you can simply do:

function foo( bar, baz ){
    console.log('Args:', arguments.join(', '))
}

然而,在 ES6 中,如果你使用箭头函数,就像这样:

However, in ES6, if you use an arrow function, like so:

const foo = ( bar, baz ) => {
    console.log('Args:', arguments.join(', '))
}

arguments 变量返回一个对象,该对象与参数无关.

The arguments variable returns an object, which is nothing close to the parameters.

所以,我想知道是否有人可以将参数传递给箭头函数?

So, I was wondering if anyone has a way to get the arguments passed to an arrow function?

我想也许我应该提供一些关于我正在努力完成的事情的信息,也许如果上述不可能,有人有更好的主意.

I guess maybe I should give some info on what I'm trying to accomplish, maybe if the above isn't possible, someone has a better idea.

基本上,我在 BluebirdJS asCallback 方法中添加了一个 IIEF,它将确定是否确实提供了回调,如果没有,则返回承诺.

Basically, I'm adding a IIEF to the BluebirdJS asCallback method, which will determine if there was actually a callback provided, if not, it returns the promise.

这是 ES5 中的一个工作示例:

Heres a working example in ES5:

var _ = require('lodash')
var Promise = require('bluebird')

function testFunc( foo, callback ) {
    return new Promise( function ( res, rej ){
        res('You Said: ' + (_.isString( foo ) ? foo : 'NOTHING') )
    })
        .asCallback((function ( args ) {
            return _.findLast(args, function(a) {
                return _.isFunction( a )
            })
        })( arguments ))
}


testFunc('test', function( err, data ) {
    if( ! _.isEmpty( err ) )
        console.log('ERR:', err)
    else
        console.log('DATA: ', data)
})
// DATA:  You Said: test

testFunc(function( err, data ) {
    if( ! _.isEmpty( err ) )
        console.log('ERR:', err)
    else
        console.log('DATA: ', data)
})
// DATA:  You Said: NOTHING

因此,如果我使用所有 ES5 函数,那效果很好,而且我不介意将它们用于 IIEF,或者在需要时使用它们.但这取决于函数内的 arguments 变量,我真的不想将其用作 ES5 函数,我更倾向于使用 ES6 箭头函数.因此,如果有一些 ES6 方法可以在 ES6 箭头函数中获取参数,那就太完美了!

So that works fine if I use all ES5 functions, and I don't mind using them for the IIEF, or inside it if needed. But this hinges on the arguments variable inside a function that I don't really want to use as an ES5 function, id rather stick to ES6 Arrow functions. So if theres some ES6 way to get arguments in an ES6 arrow function, that would be perfect!

推荐答案

箭头函数没有自己的this参数.话虽如此,您仍然可以将所有参数传递给使用Rest 参数 又名扩展运算符的箭头函数:
参考:https://strongloop.com/strongblog/an-javascript-es6-arrow-functions/

Arrow functions don't have their own this and arguments. Having said that, you can still get all arguments passed into the arrow functions using Rest parameters AKA spread operator:
Ref: https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/

function message(msg) {
  const foo = (...args) => console.log(args[0]);
  foo(`Message: ${msg}`);
}

message('Hello World'); // Message: Hello World

这篇关于使用参数变量获取传递给 ES6 箭头函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆