Why does instanceof return false for some literals?

前端 未结 10 1015
耶瑟儿~
耶瑟儿~ 2020-11-22 13:13
"foo" instanceof String //=> false
"foo" instanceof Object //=> false

true instanceof Boolean //=> false
true instanceof Object //=>         


        
10条回答
  •  攒了一身酷
    2020-11-22 13:23

    Or you can just make your own function like so:

    function isInstanceOf(obj, clazz){
      return (obj instanceof eval("("+clazz+")")) || (typeof obj == clazz.toLowerCase());
    };
    

    usage:

    isInstanceOf('','String');
    isInstanceOf(new String(), 'String');
    

    These should both return true.

提交回复
热议问题