重拾ECMAScript5基础——typeof和instanceof

instanceof只能用来判断对象和函数,不能用来判断数字和字符串等;
instanceof右边不能是undefined;
valueof检测null,数组,对象都返回object;
具体看代码:

//ECMAScript typeof and instanceof
        var obj = {};
        var arr = [];
        console.log(typeof 1);//'number'
        console.log(typeof 'a');//'string'
        console.log(typeof arr);//'object'
        console.log(typeof obj);//'object'
        console.log(typeof true);//'boolean'
        console.log(typeof null);//'object'
        console.log(typeof undefined);//'undefined'

        console.log( 1 instanceof Number);//false
        console.log( 'a' instanceof String);//false
        console.log( arr instanceof Array);//true
        console.log( true instanceof Boolean);//false
        console.log( obj instanceof Object);//true
        console.log( null instanceof Object);//false
//        console.log( undefined instanceof undefined);

你可能感兴趣的:(重拾ECMAScript5基础——typeof和instanceof)