js basic type of packaging and Math Object (VIII)

First, the basic type of packaging [ inherited type Object ]
1, Number (), String ( ), Boolean () [reference data type package type] corresponding to each of the basic data types for the number, string, boolean.

2, when using basic data types call the basic type of packaging method corresponds to the time: the background will create a basic package type of the object corresponding to the call related methods.
Corresponding procedure is as follows:
. A primary package creates a corresponding instance of the object type of a [ Packing ]
b calls the appropriate method on an instance of the object.
C after use is completed, the destruction of the instance of the object [. Unboxing ]
eg:

    var str = "abcefg" ; 
    str.substring ( 2);   // here to create an instance of String to call subString () method

3, when you create an object using Object constructor will return to the corresponding basic type of packaging according to its constructor parameter type carried by
eg:

    var obj = new new Object ( 'NZC');   // carrying the parameter string is constructed which is a String instance 
    the console.log (obj the instanceof String); // to true 
    the console.log (obj the instanceof Object); / / to true


Second, properties, and methods substantially corresponding type of packaging
1, Boolean, Number, String is not recommended to directly use the packaging type , they will increase and reduce the complexity of the code execution speed.

2, String basic data types of attributes and methods:
the number of characters of the string length [ string length ]
charAt (index) Returns the index position given string of characters of [ Returns the specified character ]
charCodeAt (i) returns a given string ASCII character encoding location [index returns the ASCII character code ]
eg:

    var s = "helloWorld!!!";
    s.charAt(1); //e
    s.charCodeAt(1); //101

indexOf (characters need to find, [the position to start searching]); [ return to index or -1 ]
from front to back to find the first occurrence of a specified character position.

lastIndexOf (characters need to find, [the position to start searching]); [ return to index or -1 ]
from the back to find the first occurrence of the string.

search (need to match string) [ Returns the index or -1 }
method of searching for a specific value of a string, and returns the matching positions:
the indexOf () and search () except that:
    search () method can not set the second Find parameters position.
    indexOf () method can not be set more powerful search value (using regular expressions to find as parameters).

concat () [ without changing the original string ]
spliced together one or more strings, and returns the new string concatenation obtained, but most use the "+" splicing [ concatenated string ]
eg:

    var str = 'hello';
    str.concat('world'); //'helloworld'
    str.concat('123','world');  //'hello123world'

[String taken not to change the string ]
slice (start position, [end position]) { return intercepted string parameter can be positive or negative ]
parameter is negative, the result with a negative argument [+ wordlength = the results are positive numerical value] value is the same
one parameter: the string returns to the start position taken terminated string
two parameters: return to start position, taken between the end position of the string, not including the end position of the character

substr (start position, [end position]) [which is similar slice () method, except that its parameters can not be negative ]

substring (start, length string taken) return [intercepted string section a parameter can be positive or negative ]
one parameter: the string returns to the start position taken terminated string
two parameters: the length of the return string corresponding to the start position taken start count
eg:

    var s = "helloworld";
    s.slice(3,7); //lowo
    s.substr(3,7); //loworld
    s.substring(3,7);//lowo

trim ();: Front and rear delete all spaces, returns the result [ change the original string ]
If you need to remove the middle of the string box can be used:

    str.split ( "") .join ( "");   // will now be followed by a string consisting of an array of space-separated string splicing aligned ye

replace () {for performing search and replace operation, to change the original string ]
(regular expression string, to be replaced) str.replace;
EG:

    "helloWorld HEllo".replace(/hello/gi,“JavaScript”); //"JavaScriptWorld JavaScript"

toLowerCase (): lowercase
toUpperCase (): is converted to uppercase

three, Math objects
comparative method
Math.min () // find the minimum of a set of numbers
Math.max () // find the maximum value in a set of numbers

the fractional value rounded to an integer number of methods
Math.ceil () rounding up [ rounded to a large value ]
Math.floor () {rounded down to take a small value to an integer ]
Math.round () rounding
eg:
Math.ceil (9.9); // 10
Math.ceil (-9.9); // -. 9
Math.floor (9.9); //. 9
Math.floor (-9.9); // - 10
Math.round (6.3) ; //. 6
Math.round (6.5); //. 7

nonce
Math.random () // returns a random number greater than 0 to less than 1

some common methods
sqrt square root (x) x a
pow (x, y) y x to the power of
absolute value ABS (x) of x
exp (x) e power of x e = 2.7182818284590


Guess you like

Origin www.cnblogs.com/nzcblogs/p/11205529.html