Object and String prototypes are not prototypes of defined strings?

后端 未结 3 866
遇见更好的自我
遇见更好的自我 2021-01-20 01:45

I\'m trying to understand how JavaScript\'s prototype-based inheritance works. I was expecting the below outputs would evaluate as true. Why is this?

         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 02:06

    This is because the string primitives are not string objects. If you want the above code to work, the string should be constructed via the String class, as in:

    var myStr = new String("Sample");
    String.prototype.isPrototypeOf(myStr); // true
    

提交回复
热议问题