js常见的内置对象有:
- 三种包装对象:String、Number、Boolean
- 其他常用的标准内置对象:Object、Array、Function、Math、Date ...
三种包装对象:--值本身是基本类型数据
例如:定义一个字符串变量,并调用chatAt()方法
- 用new方法创建包装对象
var str = new String('hello');
console.log(str); //String {'hello'}
console.log(typeof str); //object
console.log(str.charAt(1)); //e
- 不用new方法
var str0 = 'hello';
console.log(typeof str0); //string
console.log(str0.charAt(1)); //e
//不用new方法创建变量,例如str0 调用charAt()方法时,计算机内部已经经过以下操作封装处理
/* {
var str = new String('hello'); //在内部生成的是对象的形式{0: h, 1: e,...}
var str1 = str.charAt(1);
str = null;
console.log(str1); //e
} */
总结:不用new方法创建的包装类对象,虽然变量的值可以相同,但变量的类不同。直接赋值的包装对象依旧可以调用内置构造函数上的方法,其原因是计算机内部已经进行相关处理
了解内置对象上的方法可以在控制台console.log(String.prototype)或console.log(new String)