Javascript 是什么导致错误“未捕获的类型错误:数字不是函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3332137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is causing the error "Uncaught TypeError: number is not a function"
提问by hookedonwinter
I have an onchange event that updates a form, and in the updating process it calls a function to calculate shipping. I'm not sure why, but I'm getting the following error when I try to call the function:
我有一个更新表单的 onchange 事件,在更新过程中它调用一个函数来计算运费。我不知道为什么,但是当我尝试调用该函数时出现以下错误:
Uncaught TypeError: number is not a function
Uncaught TypeError: number is not a function
The function, shipping, looks like this:
函数 ,shipping看起来像这样:
function shipping( weight )
{
var flat
switch( weight )
{
case 1:
case 2:
case 3:
flat = 32.00;
break;
case 4:
flat = 18.50;
break;
case 5:
flat = 15.80;
break;
case 6:
flat = 14.00;
break;
case 7:
flat = 12.71;
break;
case 8:
flat = 11.75;
break;
case 9:
flat = 11.00;
break;
case 10:
flat = 10.40;
break;
case 11:
flat = 9.91;
break;
case 12:
flat = 9.50;
break;
case 13:
flat = 9.15;
break;
case 14:
flat = 8.86;
break;
case 15:
flat = 8.86;
break;
case 16:
flat = 8.38;
break;
case 17:
flat = 8.18;
break;
case 18:
flat = 8.00;
break;
case 19:
flat = 7.84;
break;
case 20:
flat = 7.70;
break;
} // switch
var flat_fee = flat * weight;
var mile_fee = distance * 0.90;
var shipping_fee = flat_fee + mile_fee;
simpleCart.shippingTest = shipping_fee;
return shipping_fee;
} // shipping
I'm passing in 1right now. The variable distanceis coming from an ajax call that is completed before this function is run. That function looks like this:
我现在就过去1。该变量distance来自在此函数运行之前完成的 ajax 调用。该函数如下所示:
function get_distance( zip )
{
$.getJSON(
'distance.php',
{ zip:zip },
function(json)
{
distance = json
})
} // get_distance
I've checked to make sure the variable distanceis set.
我已经检查以确保distance设置了变量。
The console says the uncaught type error is happing at the line where I call shipping(1). Any thoughts as to why that's happening?
控制台说未捕获的类型错误发生在我调用shipping(1). 关于为什么会这样的任何想法?
回答by Samuel Neff
Are you using shippingas a variable anywhere? Sounds like the functionshipping is getting overwritten by using it as a variablewith a numeric value of 1.
您是否在shipping任何地方用作变量?听起来像是function通过将其用作variable数值为 的 a 来覆盖运输1。
It's not in the code you posted (neither is the call to shipping(1)you mentioned).
它不在您发布的代码中(也不是shipping(1)您提到的电话)。

