简体   繁体   English

我需要创建一个 function 获取未知数量的参数并返回它们的乘法。 但是我得到的结果是不确定的

[英]I need to create a function which gets an unknown number of parameters and returns their multiplication. However the result which I get is undefined

I created an object and put the function in it, then used the bind method to give the function some parameters, but it is undefined in the console.我创建了一个 object 并将 function 放入其中,然后使用绑定方法给 function 一些参数,但在控制台中未定义。

let numObj = {
    multyAll(...args) {
        for (let i = 0; i < args.length; i++) {
            if (typeof args === Number) {
                arguments *= arguments[i]
                return arguments
            }
        }
    }
    }

const result = numObj.multyAll.bind(1, 2, 3)
console.log(result());

I tried another way around but still, I don't know why I think the function ignores for loop and returns the value of mul which I gave in the beginning我尝试了另一种方法,但我仍然不知道为什么我认为 function 会忽略for循环并返回我在开头给出的mul的值

let numObj = {
multyAll(...args) {
    let mul = 1
    for (arg of args) {
        if (typeof arg === Number) {
            mul *= arg
        }
        return mul
    }
    console.log(mul);
}
}

const result = numObj.multyAll.bind(1, 2, 3)
console.log(result());

Some issues:一些问题:

  • In the first version you use arguments which is a variable that gets automatically initialised with the function arguments.在第一个版本中,您使用arguments ,它是一个使用 function arguments 自动初始化的变量。 So to then use it for your product is not going to go well.因此,将它用于您的产品不会很好地用于 go。 The second version solves this point by using a different variable mul , which is properly initialised to 1.第二个版本通过使用不同的变量mul解决了这一点,该变量已正确初始化为 1。

  • In the first version, typeof args is testing the wrong variable.在第一个版本中, typeof args正在测试错误的变量。 It is not args you want to get the type of, but of args[i] .您想要获取的不是args类型,而是args[i]的类型。 In the second version this is problem is solved.在第二个版本中,这个问题得到了解决。

  • The typeof operator always returns a string, like "string", "number", "boolean", ... So that will never be equal to Number , which is a native function object, not a string. typeof操作符总是返回一个字符串,比如 "string", "number", "boolean", ... 所以它永远不会等于Number ,它是一个原生的 function object,而不是一个字符串。

  • The return should not be inside the loop, as that will immediately stop the loop. return不应该在循环内,因为这将立即停止循环。 You'll want to visit all arguments before returning, so that return must occur after the loop.您需要在返回之前访问所有arguments,因此return必须在循环之后发生。

  • bind takes as first argument the value that should serve as this during the function call. bind将在 function 调用期间应该用作this的值作为第一个参数。 The other arguments will be the normal arguments that will be passed on during the call.另一个 arguments 将是正常的 arguments,将在调用期间传递。 So you need to insert an extra argument in the first spot, like null .因此,您需要在第一个位置插入一个额外的参数,例如null

  • You should scope your variables and not have them implicitly defined as globals.您应该 scope 您的变量,而不是将它们隐式定义为全局变量。 This is the case with arg in the second version.第二个版本中的arg就是这种情况。 Insert a let or const there.在那里插入一个letconst

  • Although the JavaScript engine will insert semicolons for you automatically, the rules for this can sometimes be tricky.尽管 JavaScript 引擎会自动为您插入分号,但此规则有时可能很棘手。 It is better practice to take control over this yourself and separate your statements with semicolons.更好的做法是自己控制它并用分号分隔语句。

Here is your second version (which was the better one) with the mentioned corrections:这是您的第二个版本(这是更好的版本),其中包含提到的更正:

 const numObj = { multyAll(...args) { let mul = 1; for (const arg of args) { if (typeof arg === "number") { mul *= arg; } } return mul; } }; const result = numObj.multyAll.bind(null, 1, 2, 3); console.log(result());

Finally, this task is well suited for solving with reduce :最后,这个任务非常适合用reduce来解决:

 const numObj = { multyAll(...args) { return args.reduce((mul, arg) => mul * (typeof arg === "number"? arg: 1), 1 ); } }; const result = numObj.multyAll.bind(null, 1, 2, 3); console.log(result());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript错误地改变了简单乘法的结果。 我该如何解决? - Javascript wrongfully changes the result of a simple multiplication. How can I fix it? 我创建了一个 function 生成一个包含在变量中的随机数。 我在控制台上运行代码但未定义? - I created a function that generates a random number which is contained inside the variable. I run the code on the console yet I get undefined? 我有一个返回对象密钥的 function,但是我需要从此值访问原始 Object:Javascript - I have a function that returns an Object's key, however I need to access the original Object from this value: Javascript 我需要做一个函数,当你插入一个数字时,它能够看到哪个是最大的数字 - I need to make a function that is able to see which is the biggest number it can have when you insert a number 如何使用Javascript计算两个#ID的点击次数,并根据获得最多点击次数的#ID返回结果? - How can i use Javascript to count number of clicks on two #IDs and return a result depending which #ID gets the most clicks? 我需要一个函数来搜索数组并返回最后一个字符串后的最后一组数字? - I need a function which searches through an array and returns the last set of numbers after the last string? 我需要测试一组累积奖金结果。 我需要一个 function 如果所有结果都相同则返回 true,如果它们不同则返回 false - I need to test a set of Jackpot results. I need a function which returns true if all results are identical, and false if they are different 我想写一个 JS function ,它接受一个数字并返回一个以千 k 和百万 m 作为后缀的缩写字符串 - I want to write a JS function which takes a number and returns an abbreviated string for thousands k and millions m as suffix 我需要创建一个表单,在表单内部,有更小的表单将根据用户指定的数字生成? - I need to create a form, Inside of the form, there are smaller forms which will be generated according to user-specified number? 为什么如果我得到计算对象中的对象属性未定义而不是对象本身? 哪种方法更适合这种情况? - Why if I get the object property within the computed object gets undefined but not the object itself? Which approach fits better in this context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM