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

同理:Number, Boolean Function 类型也可以这样检测

注意:object不同

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

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

 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"

所以Object类型检测要注意构造方法。

posted on 2019-04-29 18:24  tzdy  阅读(6891)  评论(0编辑  收藏  举报