Why does instanceof return false for some literals?

前端 未结 10 831
耶瑟儿~
耶瑟儿~ 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:36

     typeof(text) === 'string' || text instanceof String; 
    

    you can use this, it will work for both case as

    1. var text="foo"; // typeof will work

    2. String text= new String("foo"); // instanceof will work

提交回复
热议问题