var strPrimitive = ‘I am a string‘; typeof strPrimitive; // string strPrimitive instanceof String; // false var strObject = new String(‘I am a string‘); typeof strObject; // Object strObject instanceof String; // true // 检查 sub-type 对象 Object.prototype.toString.call(strObject); // [object String]
从代码中可以看到,strObject 是由 String 构造函数创建的一个对象。
var strPrimitive = ‘I am a string‘; console.log(strPrimitive.length); // 13 console.log(strPrimitive.charAt(3)); // ‘m‘
可以直接在字面量上访问属性和方法,是因为引擎自动把字面量转换成 String 对象。数字字面量,布尔字面量也是如此
原文:https://www.cnblogs.com/wzndkj/p/12501624.html