未捕获的TypeError:对象5229d8fff4ae7a3803000023没有方法'toFixed'

时间:2013-09-15 03:08:20

标签: javascript jquery backbone.js backbone-views backgrid

这是我的代码段:

    json.iddd = ~~(json.id);
    console.log(typeof(json.iddd)); //shows number

    new ResponseTabView(json.iddd); // backbone view
在我打电话的视图中

this.grid = new Backgrid.Grid({body : this.body, collection :this.collection, columns: this.columns});

我收到以下错误:

Uncaught TypeError: Object 5229d8fff4ae7a3803000023 has no method 'toFixed'

如何摆脱它?

2 个答案:

答案 0 :(得分:0)

在传递给Backgrid.Grid的参数中,一个参数必须是Number类型。但是您传递的参数是'String'类型。

String对象没有toFixed()供您使用。因此错误。

console.log每个参数typoof,并阅读Backgrid的文档,了解Number类型需要的参数。

干杯!

答案 1 :(得分:0)

正如@Abhilash所说,你在toFixed()类型的对象上使用string肯定会导致错误 Object xyz has no method toFixed

toFixednumber Object不是字符串的方法。

这是您的参考表

数字对象方法

toExponential(x)    Converts a number into an exponential notation
toFixed(x)          Formats a number with x numbers of digits after the decimal point
toPrecision(x)      Formats a number to x length
toString()          Converts a Number object to a string
valueOf()           Returns the primitive value of a Number object

因此,您需要使用parstInt()parseFloat()

将字符串解析为整数
parseInt(json.iddd).toFixed();   Or
parseFloat(json.iddd).toFixed();