Changing the behaviour of the typeof operator in Javascript

前端 未结 4 609
灰色年华
灰色年华 2020-12-20 22:22

I would like to know if there is any way of overriding the behaviour of the typeof operator. Specifically, I want to return \"string\" when the typeof

4条回答
  •  心在旅途
    2020-12-20 23:01

    typeof is an operator in JavaScript so I'm quite sure you can't. To detect if something is a string you could use something like this:

    var s = "hello";
    console.log(s.substr&&s.charAt&&s.toUpperCase==="".toUpperCase)//true
    s = new String("hello");
    console.log(s.substr&&s.charAt&&s.toUpperCase==="".toUpperCase)//true
    

提交回复
热议问题