标签: arguments

如何最好地确定参数是否未发送到JavaScript函数

我现在已经看到了两种确定参数是否已传递给JavaScript函数的方法.我想知道一种方法是否优于另一种方法,或者一种方法是否使用不好?

 function Test(argument1, argument2) {
      if (Test.arguments.length == 1) argument2 = 'blah';

      alert(argument2);
 }

 Test('test');
Run Code Online (Sandbox Code Playgroud)

要么

 function Test(argument1, argument2) {
      argument2 = argument2 || 'blah';

      alert(argument2);
 }

 Test('test');
Run Code Online (Sandbox Code Playgroud)

据我所知,它们都产生了相同的结果,但我在生产之前只使用过第一个.

汤姆提到的另一个选择:

function Test(argument1, argument2) {
    if(argument2 === null) {
        argument2 = 'blah';
    }

    alert(argument2);
}
Run Code Online (Sandbox Code Playgroud)

根据胡安的评论,将汤姆的建议改为:

function Test(argument1, argument2) {
    if(argument2 === undefined) {
        argument2 = 'blah';
    }

    alert(argument2);
}
Run Code Online (Sandbox Code Playgroud)

javascript arguments

229
推荐指数
6
解决办法
14万
查看次数

C++中_tmain()和main()有什么区别?

如果我使用以下main()方法运行我的C++应用程序,一切正常:

int main(int argc, char *argv[]) 
{
   cout << "There are " << argc << " arguments:" << endl;

   // Loop through each argument and print its number and value
   for (int i=0; i<argc; i++)
      cout << i << " " << argv[i] << endl;

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到了我的期望,我的论点被打印出来了.

但是,如果我使用_tmain:

int _tmain(int argc, char *argv[]) 
{
   cout << "There are " << argc << " arguments:" << endl;

   // Loop through each argument and print its number and value
   for …
Run Code Online (Sandbox Code Playgroud)

c++ unicode arguments

222
推荐指数
4
解决办法
14万
查看次数

JavaScript函数中的默认参数值

可能重复:
如何为javascript函数创建参数的默认值

在PHP中:

function func($a = 10, $b = 20){
  // if func() is called with no arguments $a will be 10 and $ b  will be 20
}
Run Code Online (Sandbox Code Playgroud)

你怎么能用JavaScript做到这一点?

如果我尝试在函数参数中赋值,则会出错

缺少)正式参数后

javascript arguments function

222
推荐指数
4
解决办法
21万
查看次数

TypeError:method()占用1个位置参数,但给出了2个

如果我上课了......

class MyClass:

    def method(arg):
        print(arg)
Run Code Online (Sandbox Code Playgroud)

...我用来创建一个对象......

my_object = MyClass()
Run Code Online (Sandbox Code Playgroud)

......我就这样打电话method("foo")......

>>> my_object.method("foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: method() takes exactly 1 positional argument (2 given)
Run Code Online (Sandbox Code Playgroud)

...为什么Python告诉我我给了它两个参数,当我只给出一个?

python methods arguments self python-3.x

210
推荐指数
8
解决办法
41万
查看次数


bash脚本:如果参数等于此字符串,则定义一个类似于此字符串的变量

我正在做一些bash脚本,现在我有一个变量调用source和一个数组调用samples,如下所示:

source='country'
samples=(US Canada Mexico...)
Run Code Online (Sandbox Code Playgroud)

因为我想扩展源的数量(并且每个源都有自己的样本)我试图添加一些参数来做到这一点.我试过这个:

source=""
samples=("")
if [ $1="country" ]; then
   source="country"
   samples="US Canada Mexico..."
else
   echo "try again"
fi
Run Code Online (Sandbox Code Playgroud)

但是当我运行我的脚本时source countries.sh country它没有用.我究竟做错了什么?

bash scripting arguments

205
推荐指数
3
解决办法
37万
查看次数

find:缺少-exec的参数

今天我用一个命令得到了帮助,但它似乎没有起作用.这是命令:

find /home/me/download/ -type f -name "*.rm" -exec ffmpeg -i {} -sameq {}.mp3 && rm {}\;
Run Code Online (Sandbox Code Playgroud)

shell返回

find: missing argument to `-exec'
Run Code Online (Sandbox Code Playgroud)

我基本上要做的是递归遍历一个目录(如果它有其他目录)并对.rm文件类型运行ffmpeg命令并将它们转换为.mp3文件类型.完成此操作后,删除.rm刚刚转换的文件.

我很感激任何帮助.

bash shell arguments exec find

198
推荐指数
6
解决办法
20万
查看次数

参数或参数?

我常常发现自己对如何使用术语"参数"和"参数"感到困惑.它们似乎在编程世界中可以互换使用.

使用它们的正确惯例是什么?

language-agnostic parameters arguments terminology function

190
推荐指数
5
解决办法
3万
查看次数

PHP函数重载

来自C++背景;)
如何重载PHP函数?

一个函数定义是否有任何参数,另一个函数定义没有参数?在PHP中有可能吗?或者我应该使用if else来检查是否有从$ _GET和POST传递的参数?和他们联系?

php arguments overloading

186
推荐指数
6
解决办法
14万
查看次数

if [](方括号)中"[:太多参数"错误的含义

我找不到任何一个简单直接的资源,用于解释以下BASH shell错误的含义和修复,所以我发布了我在研究之后发现的内容.

错误:

-bash: [: too many arguments
Run Code Online (Sandbox Code Playgroud)

Google友好版: bash open square bracket colon too many arguments.

上下文:单个方括号中的if条件,带有一个简单的比较运算符,如equals,大于等,例如:

VARIABLE=$(/some/command);
if [ $VARIABLE == 0 ]; then
  # some action
fi 
Run Code Online (Sandbox Code Playgroud)

bash arguments if-statement

185
推荐指数
5
解决办法
22万
查看次数