简体   繁体   English

为什么typeof我的变量是一个对象,而不是一个数字

[英]Why is typeof my variable an object, and not a number

I have a table which has a couple cells that contain simple numbers (IE: 1.00, 1000.00, 10000.00). 我有一个表有几个包含简单数字的单元格(IE:1.00,1000.00,10000.00)。 I'm trying to format the cell contents using the 'format' function below. 我正在尝试使用下面的“格式”功能格式化单元格内容。 I have had success with this function in a different area of my code, but for whatever reason (the reason why I'm here) when I try to feed the contents of the table cells in, it doesn't work as I expected. 我已经在我的代码的不同区域成功使用了这个函数,但出于任何原因(我之所以在这里),当我尝试提供表格单元格的内容时,它不能像我预期的那样工作。

The problem is that the typeof my cell contents is 'object' and not 'number', so it skates right by the if statement and just returns my original value back to me. 问题是我的单元格内容的类型是'对象'而不是'数字',所以它通过if语句滑动,然后将原始值返回给我。 Is there a way I can coerce the data to be typeof number? 有没有办法可以强制数据为数字类型? I thought var n = new Number(cellText); 我以为var n = new Number(cellText); would do the trick, however, the typeof comes back as object. 会做的伎俩,然而,typeof作为对象返回。 Confused. 困惑。

In globalize.js: 在globalize.js中:

Globalize.format = function( value, format, cultureSelector ) {
    culture = this.findClosestCulture( cultureSelector );
    if ( value instanceof Date ) {
        value = formatDate( value, format, culture );
    }
    else if ( typeof value === "number" ) {
        value = formatNumber( value, format, culture );
    }
    return value;
};

In my page: 在我的页面中:

$(document).ready(function () {
    $('td[globalize="true"]').each(function () {
        var $this = $(this);
        var cellText = $this.text();
        if (cellText != null) {
            var n = new Number(cellText);
            var v = Globalize.formatNumber(n, _gloNum[0]);
            $this.text(v);
        }
    })
});

The problem is that the typeof my cell contents is 'object' and not 'number' 问题是我的单元格内容的类型是“对象”而不是“数字”

When you do: 当你这样做时:

new Number

You are creating new instance of Number object, that's why it gives you object and not number. 您正在创建Number对象的实例,这就是它为您提供对象而不是数字的原因。

Is there a way I can coerce the data to be typeof number? 有没有办法可以强制数据为数字类型?

var n = +(cellText);

Or 要么

var n = Number(cellText);

In JavaScript new Number returns a Number object. 在JavaScript中, new Number返回一个Number对象。 Have a look at parseFloat or parseInt . 看看parseFloatparseInt

Change: 更改:

var n = new Number(cellText);

To

var n = Number(cellText);

Or 要么

var n = parseFloat(cellText);

Or 要么

var n = parseInt(cellText, 10);

Depending on what you need. 根据您的需要而定。

new Number(cellText) returns a Number object, not a number primitive. new Number(cellText)返回一个Number对象,而不是一个number原语。

Use parseInt or parseFloat instead. 请改用parseIntparseFloat

var cellText = '12.34',
a = new Number(cellText), // 12.34, but a Number object
b = parseInt(cellText, 10), // 12
c = parseFloat(cellText); // 12.34

typeof a; // 'object'
a instanceof Number; // true

typeof b; // 'number'
typeof c; // 'number'

The typeof is buggy in JavaScript. typeof是JavaScript的越野车。 I suggest you use the following function instead: 我建议您使用以下功能:

function typeOf(value) {
    if (value === null) return "null";
    else if (typeof value === "undefined") return "undefined";
    else return Object.prototype.toString.call(value).slice(8, -1);
}

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

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