简体   繁体   English

为什么说 calcAverage 不是 function,而它是

[英]Why is it saying that calcAverage is not a function, when it is

I created a simple function to calcluate the average of 3 numbers:我创建了一个简单的 function 来计算 3 个数字的平均值:

let calcAverage = (v1,v2,v3) =>{

calcAverage = v1+v2+v3 / 3;

return calcAverage
}

const d1 = calcAverage(44, 23,71)
const k1 = calcAverage(44, 23,71)
console.log(d1)

if you comment out: const k1 = calcAverage(44, 23,71) then you will notice that the const d1 = calcAverage(44, 23,71) works, but why does it say 'Uncaught TypeError: calcAverage is not a function for this line of code: const k1 = calcAverage(44, 23,71) ?如果你注释掉: const k1 = calcAverage(44, 23,71)那么你会注意到const d1 = calcAverage(44, 23,71)有效,但为什么它说'Uncaught TypeError: calcAverage is not a function for这行代码: const k1 = calcAverage(44, 23,71) ? this doesn't make any sense to me.这对我来说没有任何意义。 how do I make both of them work?我如何使它们都起作用? and why is it even saying that calcAverage is not a function i the first place?为什么它甚至说 calcAverage 不是 function 我首先?

Because you reassigned calcAverage in this line因为您在这一行中重新分配calcAverage

calcAverage = v1+v2+v3 / 3;

which made calcAverage a number value instead of a function这使得calcAverage成为一个number而不是 function

why don't you just return this calculations immediately你为什么不立即返回这个计算

return v1+v2+v3 / 3;

When calling calcAverage for the first time, it indeed is a function. However, you overwrite it within the function itself to be a number :第一次调用calcAverage时,它确实是一个 function。但是,您在 function 本身内将其覆盖为一个number

calcAverage = v1+v2+v3 / 3;

Simply change the variable name inside the function to make it work as you expect:只需更改 function 中的变量名称即可使其按预期工作:

const calcAverage = (v1,v2,v3) =>{

    const result = v1+v2+v3 / 3; 

    return result;
}

const d1 = calcAverage(44, 23,71);
const k1 = calcAverage(44, 23,71);
console.log(d1);

Update:更新:

To clearly see the issue, you can log out the type of calcAverage for each of the function calls:要清楚地看到问题,您可以注销每个 function 调用的calcAverage类型:

 let calcAverage = (v1,v2,v3) =>{ calcAverage = v1+v2+v3 / 3; return calcAverage; } console.log("Type before first call: ", typeof calcAverage); const d1 = calcAverage(44, 23,71); console.log("Type before second call: ", typeof calcAverage); const k1 = calcAverage(44, 23,71);

This nicely shows the advantage of using const over using let for actual constants, by the way.顺便说一下,这很好地展示了使用const相对于使用let作为实际常量的优势。 If you would have declared calcAverage as a const , you wouldn't have run into this issue.如果您将calcAverage声明为const ,则不会遇到此问题。

i found a variable inside the function with the same function name.我在 function 中发现了一个与 function 名称相同的变量。 so when you call calAverage second time it is not a function instead it is a resultof of some calculation.因此,当您第二次调用 calAverage 时,它不是 function,而是某种计算的结果。 also i made function as a constant one我还把 function 作为常数

 const calcAverage = (v1, v2, v3) => { let calcAverageVal = v1 + v2 + v3 / 3 return calcAverageVal } const d1 = calcAverage(44, 23, 71) const k1 = calcAverage(44, 23, 71) console.log(d1)

Solution to this problem is use a different variable name to hold the calculated value inside the calcAverage arrow function.此问题的解决方案是使用不同的变量名来保存 calcAverage 箭头 function 内的计算值。

let calcAverage = (v1,v2,v3) =>{

   let calcValue = (v1+v2+v3) / 3;
   // it is recommended you bracket your numerator so as not to confuse the calcuation
   // because leaving it as v1+v2+v3 / 3 will solve the maths as v1+v2+(v3/3)

   return calcValue;
}

const d1 = calcAverage(44, 23,71);
const k1 = calcAverage(44, 23,71);
console.log(d1)

As for me, i did suggest you use const when declaring an arrow function. It helps to make the function unmanipulatable至于我,我确实建议你在声明箭头 function 时使用const 。它有助于使 function 不可操作

It is all about scope dependency都是关于 scope 的依赖

  1. in a first time you define calcAverage as a function第一次将calcAverage定义为 function
  2. then inside this function you redefine this elemenent as a number然后在这个 function 中你将这个元素重新定义为一个数字

=> on the first call: it is a function => 在第一次通话时:它是一个 function
=> on the second call: it is no more a function (it is a number) => 在第二次通话时:它不再是 function(它是一个数字)

is you want to avoid anytime this kind of problem, you have to always declare all of your variables您想随时避免此类问题,您必须始终声明所有变量

And also it is recommanded for begginners to make your code on strict mode ('use strict'; directive) wich is help you to avoid this kind of mistakes并且还建议初学者在严格模式下编写代码('use strict'; directive),这可以帮助你避免这种错误

sample code:示例代码:

 'use strict'; // on first line, for all the code let calcAverage = (v1, v2, v3) => { let calcAverage = v1 + v2 + v3 / 3; // define a 'new' locale variable //╙╨╨─────────────────────────────────// within the scope of this function return calcAverage } const d1 = calcAverage(44, 23, 71) const k1 = calcAverage(40, 23, 71) console.log(d1) // 90.666... console.log(k1) // 86.666...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM