Javascript:关于对函数参数使用or运算符的小问题 [英] Javascript: minor question about using the or operator for function parameters

查看:55
本文介绍了Javascript:关于对函数参数使用or运算符的小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带2个参数的函数。例如:



Let's say I have a function with 2 parameters. E.g.:

if (myFunction(objectA, objectB)) {
    alert("Hello! I am an alert box!!");
};





我的尝试:



如果我希望其中一个参数可能是几个对象之一(例如ObjectB或ObjectC),是否可以实现这一目标?我尝试使用OR运算符,但没有运气:



What I have tried:

If I wanted one of these parameters to possibly be one of several objects (e.g. either ObjectB OR ObjectC), is it possible to achieve this? I tried using the OR operator like so without luck:

<pre>                
                if (myFunction(objectA, objectB||objectC)) {
                    alert("Hello! I am an alert box!!");
                };

推荐答案

没有。重点是精确定义每个参数的含义,而不是引入更多歧义。



最接近的是传递列表或其他容器对象,但是然后你可以自己在你的函数中弄清楚何时以及如何使用列表中的对象。
No. The whole point is to precisely define what each parameter stands for, not to introduce more ambiguity.

The closest thing would be to pass a list or other container objects, but then you are on your own to figure out in your function when and how to use the objects in your list.


这篇关于Javascript:关于对函数参数使用or运算符的小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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