当前位置:网站首页>Why can basic data types call methods in JS

Why can basic data types call methods in JS

2022-07-07 13:08:00 Dax1_

problem

Why? string This basic data type can call methods , for example ‘a’.indexOf('a'), Isn't method invocation the ability of an object ?

answer

  • JS In order to facilitate the operation of basic data types , Provides 3 Special reference types :Boolean、Number、String, They have basic types of special behaviors
  • actually , Whenever you read a basic type ,JS Internally, a basic wrapper type object is automatically created , Let's call some methods
  • 'a'.indexOf('a') In the calling process, you will first let str = new String('a'), And then call indexOf, After calling, make str = null, Destroy the object .
原网站

版权声明
本文为[Dax1_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071100531347.html