JavaScript中的Splat运算符,相当于Python中的* args和** kwargs? [英] Splat operators in JavaScript, equivalent to *args and **kwargs in Python?

查看:336
本文介绍了JavaScript中的Splat运算符,相当于Python中的* args和** kwargs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用Python,而且我现在正在快速学习JavaScript(或者我应该说重新学习)。所以,我想问一下,在JavaScript中等价的 * args ** kwargs 是什么?

I use Python a lot, and I am just quickly learning JavaScript right now (or should I say re-learning). So, I wanted to ask, what is the equivalent of *args and **kwargs in JavaScript?

推荐答案

* args 最近的成语将是

function func (a, b /*, *args*/) {
    var star_args = Array.prototype.slice.call (arguments, func.length);
    /* now star_args[0] is the first undeclared argument */
}

利用 Function.length 这一事实是函数定义中给出的参数数量。

taking advantage of the fact that Function.length is the number of arguments given in the function definition.

你可以把它打包成一个小帮手程序,比如

You could package this up in a little helper routine like

function get_star_args (func, args) {
    return Array.prototype.slice.call (args, func.length);
}

然后再做

function func (a, b /*, *args*/) {
    var star_args = get_star_args (func, arguments);
    /* now star_args[0] is the first undeclared argument */
}

如果您想要语法糖,请编写一个函数,将一个函数转换为另一个函数,该函数使用必需和可选参数调用,并传递所需的参数,并将任何其他可选参数作为数组放在最终位置:

If you're in the mood for syntactic sugar, write a function which transforms one function into another one which is called with required and optional arguments, and passes the required arguments along, with any additional optional arguments as an array in final position:

function argsify(fn){
    return function(){
        var args_in   = Array.prototype.slice.call (arguments); //args called with
        var required  = args_in.slice (0,fn.length-1);     //take first n   
        var optional  = args_in.slice (fn.length-1);       //take remaining optional
        var args_out  = required;                          //args to call with
        args_out.push (optional);                          //with optionals as array
        return fn.apply (0, args_out);
    };
}

使用如下:

// original function
function myfunc (a, b, star_args) {
     console.log (a, b, star_args[0]); // will display 1, 2, 3
}

// argsify it
var argsified_myfunc = argsify (myfunc);

// call argsified function
argsified_myfunc (1, 2, 3);

然后,如果你愿意让来电者通过,你可以跳过所有这些mumbo jumbo可选参数作为一个数组开头:

Then again, you could just skip all this mumbo jumbo if you are willing to ask the caller to pass the optional arguments as an array to start with:

myfunc (1, 2, [3]);

确实没有类似的解决方案** kwargs ,因为JS没有关键字参数。相反,只要求调用者将可选参数作为对象传递:

There is really no analogous solution for **kwargs, since JS has no keyword arguments. Instead, just ask the caller to pass the optional arguments in as an object:

function myfunc (a, b, starstar_kwargs) {
    console.log (a, b, starstar_kwargs.x);
}

myfunc (1, 2, {x:3});



ES6更新



为完整起见,让我补充说ES6用rest参数功能解决了这个问题。请参见 http://ariya.ofilabs.com/2013/03 /es6-and-rest-parameter.html

这篇关于JavaScript中的Splat运算符,相当于Python中的* args和** kwargs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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