[英]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.在那里插入一个
let
或const
。
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.