在哪里可以获得有关 JavaScript 函数的对象参数语法的信息? [英] Where can I get info on the object parameter syntax for JavaScript functions?

查看:25
本文介绍了在哪里可以获得有关 JavaScript 函数的对象参数语法的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想调用这样的函数:

If I want to call a function like this:

moo({ a: 4 });

通常我必须像这样表述我的函数定义:

Normally I'd have to phrase my function definition like this:

function moo(myArgObj) {
    print(myArgObj.a);
}

但是这个很棒的语法在spidermonkey中完全有效用于定义函数:

But this awesome syntax is totally valid in spidermonkey for defining functions:

function moo({ a, b, c }) { // valid syntax!
    print(a); // prints 4
}

这是什么功能?

推荐答案

这叫做解构.您可能会在 MDN:解构赋值(特别是参见 从对象中解包字段作为函数参数传递).

It's called destructuring. You might find the most info at MDN: Destructuring assignment (especially see Unpacking fields from objects passed as function parameter).

ECMAScript 标准讨论可以在 他们的维基页面,同样有趣的可能是这篇博文在dailyjs.

The ECMAScript standards discussion can be found on their wiki page, also interesting might be this blog post at dailyjs.

这篇关于在哪里可以获得有关 JavaScript 函数的对象参数语法的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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