You do not know the JS series (28) - built-in objects

There are also a subtype JavaScript objects, often referred to as built-in objects

 

String
Number
Boolean
The names of the three built-in objects seem simple and basic types, in fact, their relationship is more complex

 

Object
Function
Array

 

Data
RegExp
Error

 

These built-in functions can be used as a constructor, to construct a new object corresponding to subtype

 

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]

You can see from the code, strObject is a String object created by the constructor.

 

Original value 'I am a string' is not an object, it is only a literal, and is an immutable value, if you want to perform some operation on the literal, the length of such acquisition, wherein accessing a character, etc., need convert it to a String object.

 

var strPrimitive = 'I am a string';
console.log(strPrimitive.length); // 13
console.log(strPrimitive.charAt(3)); // 'm'

Properties and methods can be accessed directly on the literal, since the engine is automatically converted into a literal String object. Digital literal, Boolean literal, too

Guess you like

Origin www.cnblogs.com/wzndkj/p/12501624.html