在JavaScript中跳过可选的函数参数 [英] Skipping optional function parameters in JavaScript

查看:514
本文介绍了在JavaScript中跳过可选的函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请指点我在JavaScript中跳过可选参数的好方法。

Could you please point me to the nice way of skipping optional parameters in JavaScript.

例如,我想扔掉所有 opt_ 这里的参数:

For example, I want to throw away all opt_ parameters here:

goog.net.XhrIo.send(url, opt_callback, opt_method, opt_content, {'Cache-Control': 'no-cache'}, opt_timeoutInterval)


推荐答案

解决方案:

goog.net.XhrIo.send(url, undefined, undefined, undefined, {'Cache-Control': 'no-cache'})

你应该使用 undefined 而不是要跳过的可选参数,因为这100%模拟了JavaScript中可选参数的默认值。

You should use undefined instead of optional parameter you want to skip, because this 100% simulates the default value for optional parameters in JavaScript.

小例子:

myfunc(param);

//is equivalent to

myfunc(param, undefined, undefined, undefined);

强烈推荐:如果您有很多参数,请使用JSON,以及您可以在参数列表中间包含可选参数。看看如何在 jQuery 中完成此操作。

Strong recommendation: use JSON if you have a lot of parameters, and you can have optional parameters in the middle of the parameters list. Look how this is done in jQuery.

这篇关于在JavaScript中跳过可选的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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