javascript判断是否为字符串类型的方法:使用typeof操作符返回未经计算的操作数的类型,使用constructor属性返回对创建此对象的Boolean函数的引用。然后通过“==”判断这两个返回值是否为string即可。
判断是否为字符串:
<script type="text/javascript">
//<![CDATA[
document.write(isString('test'),'<br/>');
document.write(isString(10),'<br/>');
function isString(str){
return (typeof str=='string')&&str.constructor==String;
}
//]]>
</script>constructor 属性:
constructor 属性返回对创建此对象的 Boolean 函数的引用。
语法:
object.constructor
typeof 运算符:
typeof 操作符返回一个字符串,表示未经计算的操作数的类型。
语法:
typeof 运算符后接操作数:
typeof operand typeof(operand)
参数:
operand:一个表示对象或原始值的表达式,其类型将被返回。






网友评论文明上网理性发言 已有1人参与
发表评论:
评论列表