javascript--Basic Wrapper Type +math Object

Source: Internet
Author: User
Tags natural logarithm square root

1. Basic Packaging type
1) To facilitate operation of the base type value, ECMAScript provides 3 special reference classes, Boolean, number, String
Whenever a primitive type value is read, the background creates a corresponding basic wrapper type object, which allows us to invoke some methods to manipulate the data.
var s = "Briup";
S.substring (2);
The following actions are done automatically in the background:
A. Creating an instance of type string
B. Invoking the specified method on an instance
C. Destruction of this instance
2) instance invocation of the basic wrapper type typeof returns "Object" so that all basic wrapper type objects are converted to Boolean type true.
The object constructor, like a factory method, returns an instance of the corresponding base wrapper type based on the type of the value passed in.
var obj = new Object ("Briup"); Obj type is a string wrapper type
Console.log (obj instanceof String);
Constructors that use new to invoke the base wrapper type are not the same as a conversion function that calls the same name directly
var s = "11";
var S1 = number (s); Transformation function Number Type
var s2 = new number (s); Constructor Object Type
3) Boolean,number, we do not recommend the use of these two types directly
4) String
1. Common functions and properties
Length
property, gets the number of characters in the string
CharAt (i)
Returns the character at the given position
charCodeAt ()
Returns the character encoding of the character at the given position
var s = "HelloWorld";
S.charat (1); E
S.charcodeat (1); 101
Concat ()
Concatenation of one or more strings, returning a new string of stitching, but mostly using "+" stitching.
Slice ()
(Start position, return character position after characters)
SUBSTR ()
(Start position, number of characters returned)
SUBSTRING ()
(Start position, return character position after characters)
var s = "HelloWorld";
S.slice (3,7); Lowo
S.substr (3,7); Loworld
S.substring (3,7);//lowo
s//helloworld does not change the original value size
IndexOf ();
Finds the position of the specified character, returns the first position that matches the character, and returns 1 if it is not found.
LastIndexOf ();
Looking at the position of the string from the back forward, you can have a second argument that represents where to start looking from within the string.

var str = "Hellowroldtoday";

Console.log (Str.indexof ("J"));    //4
   console.log (Str.lastindexof ("O"));      //11
   trim ();   
     Delete all spaces in the front and back, return the result
     var s = "Hello World";
     Console.log ("|" +s.trim () + "|");  //|hello world|
   tolowercase ()  : Convert to lowercase
   touppercase ()  : Convert to uppercase

Support for regular expressions in string   2.javascript      
   search ()   The
     parameter is a regular expression. If the argument is not a regular expression, it is first converted to a constructor by RegExp. Global retrieval is not supported, returns the position of the first substring to match, and returns 1 if no matching substring is found.
      JavaScript. Search (/script/i);//Return 4
   replace ()
      used to perform retrieval and substitution operations. The first argument is a regular expression, and the second argument is the string to replace.
     text.replace (/javascript/gi, "JavaScript");//Case insensitive convert all JavaScript to JavaScript
   match ()   
     most commonly used regular expression methods, parameters are regular expressions. Returns an array that consists of matching results.
     is not a global match when there is no G modifier in the regular expression. At this point, the first element of the array is a matching string, and the remaining elements are sub-expressions enclosed in parentheses in the regular expression. If the regular expression is set to the modifier g, the array returned by the method contains all the matching results in the string.
     1 plus 2 equals 3. Match (/\d+/g)//Return ["1", "2", "3"]

var url =/(\w+): \/\/([\w.] +) \/(\s*)/;
var text = "Visit my blog at Http://www.briup.com/~ee";
var result = Text.match (URL);
if (result!=null) {
var fullurl = result[0];
var protocol = result[1];
var host = result[2];
var path = result[3];
}
Split () method for splitting a string
Parameter returns a string array for a string or regular expression
"1, 2, 3, 4, 5". Split (/\s*,\s*/); ["1", "2", "3", "4", "5"] allow whitespace left and right on both sides of the delimiter

2. Math Object
1) Common methods
1. Comparison method
Math.min () to find the minimum value in a set of numbers
Math.max () to find the maximum value in a set of numbers
Math.min (1,2,19,8,6); 1
2. Several ways to round a decimal number to an integer:
Math.ceil () rounding up
Math.floor () rounding down
Math.Round () rounding
Console.log (Math.ceil (12.41)); 13
Console.log (Math.floor (12.41)); 12
Console.log (Math.Round (12.3)); 12
Console.log (Math.Round (12.5)); 13
3. Random number
Math.random () returns a random number greater than 0 and less than 1
2) Other methods: (Understand, instant check)
ABS (num) returns the absolute value of num
EXP (num) returns the NUM power of MATH.E
LOG (num) returns the natural logarithm of num
POW (num,power) returns the power sub power of num
sqrt (num) returns the square root of num
SCOs (x) returns the inverse cosine value of x
ASIN (x) returns the inverse sine value of x
Atan (x) returns the inverse tangent value of x
ATAN2 (y,x) returns the inverse tangent value of y/x
COS (x) returns the cosine of X
Sin (x) returns the sinusoidal value of x
Tan (x) returns the tangent of X

javascript--Basic Wrapper Type +math Object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.