相关疑难解决方法(0)

在JavaScript中将数组作为函数参数传递

我想使用数组作为参数来调用函数:

const x = ['p0', 'p1', 'p2'];
call_me(x[0], x[1], x[2]); // I don't like it

function call_me (param0, param1, param2 ) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

有路过的内容的一种更好的方式x进入call_me()

javascript arrays function call

279
推荐指数
7
解决办法
47万
查看次数

在Javascript中使用动态参数调用动态函数

我正在寻找一个关于这个的伎俩.我知道如何在Javascript中调用动态的任意函数,传递特定的参数,如下所示:

function mainfunc(func, par1, par2){
    window[func](par1, par2);
}

function calledfunc(par1, par2){
    // Do stuff here
}

mainfunc('calledfunc', 'hello', 'bye');
Run Code Online (Sandbox Code Playgroud)

我知道如何在mainfunc中使用arguments []集合传递可选的无限参数,但是,我无法想象如何向mainfunc发送任意数量的参数以动态发送到calledfunc ; 我怎么能完成这样的事情,但有任意数量的可选参数(不使用丑陋的if-else)?:

function mainfunc(func){
    if(arguments.length == 3)
        window[func](arguments[1], arguments[2]);
    else if(arguments.length == 4)
        window[func](arguments[1], arguments[2], arguments[3]);
    else if(arguments.length == 5)
        window[func](arguments[1], arguments[2], arguments[3], arguments[4]);
}

function calledfunc1(par1, par2){
    // Do stuff here
}

function calledfunc2(par1, par2, par3){
    // Do stuff here
}

mainfunc('calledfunc1', 'hello', 'bye');
mainfunc('calledfunc2', 'hello', 'bye', 'goodbye');
Run Code Online (Sandbox Code Playgroud)

javascript function

149
推荐指数
5
解决办法
20万
查看次数

Math.max.apply()如何工作?

Math.max.apply()工作怎么样?

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <script>
      var list = ["12","23","100","34","56",
                                    "9","233"];
      console.log(Math.max.apply(Math,list));    
  </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

上面的代码在List中找到Max number.任何人都可以告诉我下面的代码是如何工作的?如果我通过它似乎有效null or Math.

console.log(Math.max.apply(Math,list));
Run Code Online (Sandbox Code Playgroud)

是否所有user-defined/Native functions有可以使用的电话和申请方法?

javascript function apply

73
推荐指数
4
解决办法
6万
查看次数

如何将所有参数作为集合传递给另一个函数而不是作为单个参数?

我有一个接受任何数量和种类的参数的函数,因此没有定义任何特定的参数.此函数应调用另一个传递所有参数的函数.

问题是我可以传递"参数"以包含所有参数,但在这种情况下,它将像单个参数一样工作,而不是我们期望参数工作的方式.

一个例子:

主要功能:

function handleCall() {
   // let's call a sub-function
   // and pass all arguments (my question is how this is handled the right way)
   function callSubFunction( arguments );
}

function callSubfunction( userid, customerid, param) {
   // passed arguments are now 
   alert( 'userid = ' + userid );

   // this will not work, you have to use arguments[2]
   alert( param );
  }

The example call:

handleCall( 1029, 232, 'param01' );
Run Code Online (Sandbox Code Playgroud)

使用上面的方法,所有参数将作为伪数组存储在"userid"中,并且可以访问项目,例如arguments [2]但不使用参数名称"param".

在ColdFusion中,这种东西的解决方案是参数"argumentCollection",这样您就可以传递存储在结构中的参数,而不会转换为包含所有键/值的类型struct的单个参数.

我怎样才能用JavaScript实现同样的目标?

javascript

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

Javascript:转发带有可变数量参数的函数调用

我想我需要像ruby's splat这样的东西*.

function foo() {
  var result = '';
  for (var i = 0; i < arguments.length; i++) {
    result += arguments[i];
  }
  return result;
}

function bar() {
  return foo(arguments) // this line doesn't work as I expect
}

bar(1, 2, 3);
Run Code Online (Sandbox Code Playgroud)

我希望这回来"123",但我得到了"[object Arguments]".我认为这是有道理的.它传递的是表示参数的对象,而不是单独的参数.

那么我如何简单地将任意数量的参数转发给另一个带有任意数量参数的函数呢?

javascript arguments

10
推荐指数
1
解决办法
5314
查看次数

Javascript将变量参数传递给超类构造函数

将变量参数传递给超类构造函数的最佳/推荐方法是什么? 背景解释了我试图解决的问题.

背景

我正在将一些代码从Java移植到Javascript.Java具有的编码模式之一是函数重载.Java选择最佳匹配来确定要调用的函数.当函数是Class构造函数方法时,这会变得很有趣.

所以Java中的代码可能是

public class MyParser extends Parser {
    public int parse(String str) { super(str); ... } 
    public int parse(String str, int base) { super(str, base); ... }
}
Run Code Online (Sandbox Code Playgroud)

在Javascript中变为:

class MyParser extends Parser {
    constructor(){
        super(arguments); // THIS DOES NOT WORK
        if (arguments.length === 1 && typeof arguments[0] === 'string'){
            constructor_1arg(arguments[0]);
        } else if (...){
            constructor_2args(arguments[0], arguments[1]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我想知道处理这种情况的最佳方法是什么?

可能解决方案

注意:我问这个是因为调用super()是构造函数中的一个特例,你只能调用一次.所以我没想到这会起作用,但确实如此(在NodeJS v8.2中).

class MyParserTry2 extends Parser {
    constructor(){
        console.log('MyParser2 passed ' + arguments.length + ' …
Run Code Online (Sandbox Code Playgroud)

javascript

7
推荐指数
1
解决办法
1851
查看次数

如何将参数数组传递给JavaScript中的另一个函数?

我有一个JavaScript函数:

function test() {
  console.log(arguments.length);
}
Run Code Online (Sandbox Code Playgroud)

如果我打电话给它test(1,3,5),它打印出来,3因为有3个参数.如何从另一个函数中调用test并传递其他函数的参数?

function other() {
  test(arguments); // always prints 1
  test(); // always prints 0
}
Run Code Online (Sandbox Code Playgroud)

我想调用othertest并使用它的arguments数组调用它.

javascript arguments function

4
推荐指数
1
解决办法
4400
查看次数

将一个函数的所有参数传递给另一个函数?(JavaScript的)

如何将一个函数的所有参数传递给另一个函数

function a(){
    arguments.length;// this is 1
    // how do I pass any parameter function a receives into another function
    b(what to put here?); // say pass all parameters function a receives to function b
}
a('asdf'); // this is okay
Run Code Online (Sandbox Code Playgroud)

因此,如果函数a接收X个参数,则每个参数以相同的顺序传递到函数b.所以如果a("1","2"); ,b("1","2");,如果a("1",2,[3]); ,b("1",2,[3]);.

javascript

3
推荐指数
1
解决办法
5286
查看次数

JS的"论据"特性让他们感到非常困惑

我有这个电话:

function del(Model, modelName, model_id, req, res, next, cb) {

    if(req.query.optimisticDelete){
        optimisticDelete(arguments);
    }
    else{
        pessimisticDelete(arguments);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题当然是,参数没有正确传递给optimisticDelete和pessimisticDelete函数.

在一个理想的JS世界中,这可能有用,但我很容易理解为什么它没有.

但它并没有消除这一事实,我只是不想为每个调用输入所有参数,事实上我也想省略del函数签名中的参数,所以这将是最理想的虽然我需要对req对象的引用,但我现在缺少了:

  function del() {

        if(req.query.optimisticDelete){
            optimisticDelete(arguments);
        }
        else{
            pessimisticDelete(arguments);
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是当然,当传递参数时,它似乎并没有神奇地分离成单独的参数.

而且,这也不起作用:

function del(Model, modelName, model_id, req, res, next, cb) {

    if(req.query.optimisticDelete){
        optimisticDelete(Array.prototype.slice.call(arguments));
    }
    else{
        pessimisticDelete(Array.prototype.slice.call(arguments));
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您了解我想要做什么,请告诉我是否可能以及如何,

javascript

2
推荐指数
2
解决办法
118
查看次数

标签 统计

javascript ×9

function ×4

arguments ×2

apply ×1

arrays ×1

call ×1