JavaScript中如何检测一个变量是一个String类型?

1 typeof x === "string" 2 typeof(x) === "string' // 小写 3 x.constructor === String // 大写类型

同理:Number, Boolean Function 类型也能够这样检测函数

注意:object不一样this

var obj = { 'age':19, 'put':function(){ return 0; } }

这样定义的对象与前面规则同样spa

 var Obj = function(){ this.age = 12; this.put = function(){ return 0; } } var obj = new Obj; typeof obj = "object"

经过构造函数构建对象,obj.constructor = ƒ Function() { [native code] }      typeof obj = "object"code

因此Object类型检测要注意构造方法。对象

相关文章
相关标签/搜索