1. To facilitate operation of the basic type values, the ECMAScript also provides 3 special types of the type: Boolean, number, Striung.
These types are similar to other reference types, but also have the corresponding special behavior of their own primitive types, and in fact each time a basic type is read,
The background creates an object of the corresponding basic wrapper type, which allows us to invoke some methods to manipulate the data.
2. For example.
<! DOCTYPE html><body> <!--Basic Package type, the background creates an object of the corresponding basic wrapper type whenever a basic type is read--<!--1. Creates a string instance, 2. Invokes the specified method on the instance 3. Destroys this instance. -<script>//The instance of a reference type created with the new operator is persisted in memory until it is executed to the left scope var S1 = new String ("some text"); The basic string is converted to an object, var s2 = s1.substring (2); Console.log (S2);
The objects that are automatically created by the basic package type are then immediately destroyed by a single line of code, var s1 = "some text"; S1.color = "Red"; Console.log (S1.color); underfined//Basic wrapper type instance call typeof will return "" Object, and all basic wrapper types will be converted to Boolean true//pass string to the object function to create an instance of string,// The passed parameter will get the instance of number, and the Boolean parameter will get the Boolean instance var obj = new Object ("some text"); Console.log (obj instanceof String); True
var value = "25"; var number = number (value); Transformation function Console.log (number); Number var obj = new number (value); Console.log (typeof obj); Object
A Boolean type whose instance overrides the ValueOf () method, returns the base type value of true Falue//The object in the ideal expression will be converted to true var booleanobject = new Boolean (true); var falseobject = new Boolean (false); False will be converted to true var result = Falseobject && true; Console.log (result); True
var falsevalue = false; result = Falsevalue && true; Console.log (result); False
The number type//Nunber type also overrides valueof (), tolocalestring (), and the ToString () method//value () returns the numeric value of the base type represented by the object, and the other two methods return the value var in string form Nunberobject = new number (10); var num = 10; Console.log (Num.tostring ()); Console.log (num.tostring (2)); Console.log (num.tostring (8)); Console.log (num.tostring (10)); The method of formatting a number into a character representable var num = 10; Console.log (num.tofixed (2)); 10.00, with two-bit decimal var num = 10.005; Console.log (num.tofixed (2)); 10.01 rounding
STIRNG type String Object wrapper type var stringvalue = "Hello World"; Console.log (stringvalue.length); 11//Character Method CharAt () charCodeAt () var stringvalue = "Hello World"; Console.log (Stringvalue.charat (1)); e var stringvalue = "Hello World"; Console.log (Stringvalue.charcodeat (1)); 101 Small Letter E encoded Console.log (stringvalue[1]); E//String manipulation Method Concat () string concatenation, concat () can accept multiple parameters, var stringvalue = "Hello"; var result = Stringvalue.concat ("World"); Console.log (result); Hello World Console.log (stringvalue); Hello
Slice () substr () substring (), accepts one or two parameters, the first start position, and the second end position. var stringvalue = "Hello World"; Console.log (Stringvalue.slice (-3)); Rld//substring () will convert negative values to 0 console.log (stringvalue.substring (-3)); The string position method indexof (), LastIndexOf (), in the opposite direction//space is also counted as a character var stringvalue = "Hello World"; Console.log (Stringvalue.indexof ("H")); Starting from 0 console.log (stringvalue.indexof ("O")); 4 Console.log (Stringvalue.lastindexof ("O")); 7
var stringvalue = "Hello World"; Console.log (Stringvalue.indexof ("O", 6)); 7 Console.log (Stringvalue.lastindexof ("O", 6)); 4
var stringvalue = "Lorem ipsum dolor sit amet,consectetur adipisicing slit"; var positions = new Array (); var pos = stringvalue.indexof ("E"); while (Pos >-1) {Positions.push (pos); pos = Stringvalue.indexof ("E", pos + 1);
} console.log (positions); Trim () method, delete the copy of a string before and after all the spaces var stringvalue = "Hello World"; var trimmedstringvalue = Stringvalue.trim (); Console.log (StringValue); "Hello World"; Console.log (Trimmedstringvalue); Hello World
String size conversion method var stringvalue = "Hello World"; Console.log (Stringvalue.tolocaleuppercase ()); HELLO World Console.log (Stringvalue.touppercase ()); HELLO World Console.log (Stringvalue.tolocalelowercase ()); Hello World Console.log (Stringvalue.tolowercase ()); Hello World
String pattern matching method//The match () method receives a parameter either a parameter or a regular expression//Match () method returns an array of var text = "Cat, bat, Sat, fat"; var pattern =/.at/; var matches = Text.match (pattern); Console.log (Matches.index); 0 Console.log (matches[0]); Cat Console.log (PATTERN.LASTINDEX); 0//search () method The only parameter is the same as match () var text = "Cat, bat, Sat, fat"; var pos = Text.search (/at/); Console.log (POS); 1 at the first occurrence of the position.
Replace () method, receive two functions, the first parameter can be a regular object can also be a string,///The second function can be a string or it can be a function var text = "Cat, bat, Sat, fat"; var result = Text.replace ("At", "ond"); Console.log (result); Cond, bat, Sat, fat replacement, result = Text.replace (/at/g, "ond"); Global replacement Console.log (result); Cond, Bond, Sond, fond var text = "Cat, bat, Sat, fat"; result = Text.replace (/(. at)/g, "Word ($)"); Console.log (result); Word (cat), Word (BAT), Word (Sat), Word (FAT)//split (); divides characters into strings and puts them in an array var colortext = "Red,blue,green,yellow"; var colors1 = Colortext.split (","); var colors2 = Colortext.split (",", 2); var colors3 = Colortext.split (/[^\,]+/); Console.log (COLORS1); ["Red", "Blue", "green", "yellow"] console.log (COLORS2); ["Red", "Blue"] Console.log (COLORS3); //["", ",", ",", ",", ""]
Localecompare () method, this method compares two strings and returns the result//1. The string should return a negative number//2 before the character-producing parameter in the alphabet. String equals string parameter, Returns//3. If the string should be in the alphabet after the string argument, Returns a positive number var stringvalue = "Yellow"; Console.log (Stringvalue.localecompare ("Brick")); 1 Console.log (Stringvalue.localecompare ("yellow")); 0 Console.log (Stringvalue.localecompare ("Zoo")); -1 </script></body>
JavaScript basic packaging types and how to operate them