Javascript 如何测试值是字符串还是int?
可能重复:Javascript 如何测试值是字符串还是int?,javascript,Javascript,可能重复: 如何测试值是字符串还是int?类似于 X= 如果X是Int{} 如果X是字符串{} 谢谢 您可以使用运算符: var x = 1; console.log(typeof x); x = 'asdf'; console.log(typeof x); 印刷品: number string 大部分时间都是这样。但是如果您的数字或字符串不是原语,它将返回对象。一般来说,这不是你想要的 var str = new String('Hello'); typeof str; // 'obje
如何测试值是字符串还是int?类似于 X= 如果X是Int{} 如果X是字符串{} 谢谢 您可以使用运算符:
var x = 1;
console.log(typeof x);
x = 'asdf';
console.log(typeof x);
印刷品:
number
string
大部分时间都是这样。但是如果您的数字
或字符串
不是原语,它将返回对象
。一般来说,这不是你想要的
var str = new String('Hello');
typeof str; // 'object'
typeof
还表示null
是一个对象
,在WebKit中,正则表达式是一个函数
。我认为typeof
的主要优点是检查变量而不抛出ReferenceError
您也可以检查变量的构造函数
属性,或者使用变量的字符串实例
。但是,当使用交叉窗口
代码时,这两种方法在多个窗口
环境中都不起作用
确定类型的另一种保证方法是使用
var getType = function(variable) {
return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase();
}
.使用JavaScript内置的
typeof
函数
var s = "string",
n = 1; // number
if(typeof s == 'string'){
//do stuff for string
}
if(typeof n == 'number'){
//do stuff for number
}
这里有一个函数支持
typeof
,但在需要时默认为Object.prototype.toString
(这要慢得多)
通过这种方式,您将从新字符串('x')
或null
或/regex/
(在Chrome中)获得一些意外值
var type = (function () {
var toString = Object.prototype.toString,
typeof_res = {
'undefined': 'undefined',
'string': 'string',
'number': 'number',
'boolean': 'boolean',
'function': 'function'
},
tostring_res = {
'[object Array]': 'array',
'[object Arguments]': 'arguments',
'[object Function]': 'function',
'[object RegExp]': 'regexp',
'[object Date]': 'date',
'[object Null]': 'null',
'[object Error]': 'error',
'[object Math]': 'math',
'[object JSON]': 'json',
'[object Number]': 'number',
'[object String]': 'string',
'[object Boolean]': 'boolean',
'[object Undefined]': 'undefined'
};
return function type(x) {
var the_type = typeof_res[typeof x];
return the_type && (the_type !== 'function' || (x.apply && x.call)) ?
the_type :
tostring_res[toString.call(x)] || (x ? 'object' : 'null');
};
})();
编辑:我刚刚重写了,删除了函数的一个关键部分。固定的 或者对于更紧凑的版本:
var type = (function() {
var i, lc, toString = Object.prototype.toString,
typeof_res = {},
tostring_res = {},
types = 'Undefined,String,Number,Boolean,Function,Array,Arguments,RegExp,Date,Null,Error,Math,JSON'.split(',');
for (i = 0; i < types.length; i++) {
lc = types[i].toLowerCase();
if (i < 5) typeof_res[lc] = lc;
tostring_res['[object ' + types[i] + ']'] = lc;
}
return function type(x) {
var the_type = typeof_res[typeof x];
return the_type && (the_type !== 'function' || (x.apply && x.call)) ?
the_type :
tostring_res[toString.call(x)] || (x ? 'object' : 'null');
};
})();
var type=(函数(){
变量i,lc,toString=Object.prototype.toString,
typeof_res={},
tostring_res={},
类型='Undefined,String,Number,Boolean,Function,Array,Arguments,RegExp,Date,Null,Error,Math,JSON'.split(',');
对于(i=0;i
其他人已经讨论了运算符的类型以及Object.prototype.toString
的使用,但是如果您想专门测试int与任何类型的数字的比较,那么您可以做一些变化:
function isInt(n) {
return typeof n === "number" && n === Math.floor(n);
}
(如果需要,插入所有Object.prototype.toString
内容。)如果您描述了您真正想要解决的问题,我们可能会提供更好的帮助。我记得大约一个月前您向我解释过这一点。typeof str;//'编号“
?那个一定是IE5的。)
function isInt(n) {
return typeof n === "number" && n === Math.floor(n);
}