JavaScript basic Syntax &2

Source: Internet
Author: User

7.number && Data conversion to number type
Initialization
var number = new Number (' 23 ');//Use the constructor to create * use the TypeOf operator to return ' object '; Use the type function to return ' number ';
var number = 23; Direct volume
Method:
Number.tofixed ();//Keep a few decimals, the result will be rounded; for example: 1.0263.toFixed (2); The result is "1.03";
(8). toString (n); convert to n-binary string;
8..toString ();
Conversion type:
There are three ways to convert something else to a number:
Number (value) && +value;
Returns the value directly for the number type;
is a Boolean type,true->1;false->0;
Is null, returns 0;
For undefined, return nan;
As String:
An empty string returns 0;
If there is only a number in the string (which can contain a leading symbol), it is converted directly to a number and ignored if there is a leading zero;
If the string contains a floating-point format (2.3), the rule is as above;
If the string contains 16 binary format (0xf), it is converted to a hexadecimal integer value of the same size;
Returns NaN if the string contains other format characters;
If it is an object, then the valueof () method is called, the return value is converted according to the above rule, and if it is Nan, the ToString () method is called, which is converted according to the preceding rule;
Paseint (value) &&parsefloat (Value,system);
Converts the value of system binary to a number of 10 binary;
Return Nan when empty string
The first character is not a number or a positive sign, and a nan is returned
The first character is the number of times the second is processed, until it is not a number; Example: ' 152abc,sda12 ' = 152
Do not consider the decimal point to be a valid character, example: ' 10.12 ' + 10

8.string && Convert to String type, action string
To create a string:
var string = new String (' Alibaba '); constructor function;
var string = ' Alibaba '; Direct volume, quotation marks can be double and single-lead;
var string = ' ali\ ' Baba '; = Ali ' Baba ' is escaped;
var string = ' non-printable character: \ n, \ r, \ t ';
var string = ' using hexadecimal notation, \x + nn: \x41 ';
var string = ' \u + nnnn: \u0061 '; use Unicode notation;
Convert to String:
string (value);
"' +value;
(number). toString (sys); A string converted to a sys binary;
Properties and methods of string:
length = lengths;
String[n] + accesses the n+1 character inside the string;
Concat () = Merge string is generally used directly with +;
Replace (key, value) = = Replaces key with Value,key can be a regular expression;
IndexOf (value) = = Starts from the previous lookup value and returns the subscript for the first character of value; If 1 is not found;
LastIndexOf (value) =-finds and returns the subscript of the first character of value, starting from the back; if 1 is not found;
Search () = finds value and returns the subscript of the first character of value; If 1 is not found;
Match (value) = Returns the [value] length of 1 if the argument is a string and returns an array of all matching elements if the regular expression;
Slice ()/substing () \substr () = intercepts a string, receives two parameters begin,end (optional, does not intercept the last character); Returns a string containing a begin subscript that does not contain an end subscript; The parameter can be a negative number;
Split () = uses characters or regexp to cut strings for parameters, generating arrays;
toUpperCase () = converts lowercase letters to uppercase;
toLowerCase () = converts capital letters to lowercase letters;
CharAt (number) = The character that returns the position of number;
charCodeAt (number) = Unicode encoding of the character that returns the number position;
String.fromCharCode (Unicode) = static method, returns the Unicode corresponding character;
Trim () = remove spaces at both ends of the string;
Trimleft () = Remove the space to the left of the string;
TrimRight () = Remove the space to the right of the string;
StartsWith (str) = = Determines whether a string starts with Str
EndsWith () = Determines whether the string ends with Str
Repeat (number) = Tile the string number times;
Contains (val) = = Determines whether the string contains Val (); The lookup method can be substituted, as long as the returned value is not-1, which is included;
Codepointat (sub) = Returns the encoding of the character corresponding to the sub, which supports more characters than charCodeAt ();
String.formcodepoint (val) = static method, which returns the corresponding character of the encoding;
9.Blooean && Convert to Blooean type
Create:
var flag = new Blooean (true);
var flag = true; Direct volume;
Convert to Boolean type method:
Boolean (Val);
!! Val ();
The IF (val) = = will implicitly convert Val;
10.Array && Array Creation and operation
Create:
var array = new Array (val); Example: var array = new Array (2); Creates an array of length 2; [Undefined,undefied];
var array = new Array (2, 3); = = [2, 3];
var array = new Array (' ABC ', ' Def ', ' 456 '); = = [' abc ', ' Def ', ' 456 '];
var array = [2, 3, ' abc '] + = direct volume;
var array = ' A,b,c,d '. Split (', ');
Properties and methods of the array:
[].length = length, non-read-only attribute; Example: var arr = [A, b, c]; Arr.length = 2; = Arr = [A, b];
Push (); = = Inserts an element from the end of the array; Vaar a = [1, 2, 3].pop (); + A = 3;
Unshift (); + = insert element from the head of the array; var arr arr = [1, 2, 3]; Arr.push (4, 5, 6); = arr = [1, 2, 3, 4, 5, 6];
Pop (); = = Delete and return the last element of the array; vr = []; Arr.unshift (1, 2, 3) = arr = [1, 2, 3];
Shift (); + = Deletes and returns the first element of the array; var a = [1, 2, 3].shift (); + A = 1;
Slice (); = = Intercept array similar to String, var arr = [1, 2, 3, 4, 5],arr1 = Arr.slice (2,-1); = = ARR1 = [3, 4];arr unchanged;
Splice (); =>var arr = [1, 2, 3]; Arr.splice (1, 1, ' A ', ' C '); = = Arr = [1, "A", "C", 3];
Concat (Val); = = generates a new array, obtaining the value of two arrays (which can also be string,obj,number), with the values of the parameters placed at the end of the array;
Join (Val); = = Convert the array to a Val-separated string; Example: var arr = [1, 2, 3]; Arr.join ("); "123"; Arr.join ('); 1 2 3 ";
Reverves (); = = Reverse array order;
Sort (); = = sort array; You can receive a function as a parameter;
IndexOf ();
LastIndexof (); = = A string-like lookup method; Returns the position of the element in the array, or 1 for an array that does not have this element;
Array.isarray (); = = Judgment is not an array; similar to IsNaN ();
New method:
ForEach (); = = Loop array;
Every (func) + = Whether the array satisfies the Func condition, or False if one is not satisfied;
Some () = Returns true if one satisfies;
Map () =
Filter () = Returns a new array that satisfies the condition;
Reduce () =
Reduceright () =

11.Object && creation and operation;
Categories of objects:
Math = The object of a mathematical operation;
Date = = Dates object;
Fucntion = = function object; All functions can be regarded as an instance of it;
Object = The source of all objects;
RegEXP = = Regular expression;
Host object:
Window
top = Ifarm access to the external window object;
Location
History
Custom objects:
var obj = {};
Initialization
var obj = new Object ();
var obj = {};
The properties and methods of the object;
Constructor = pointer;
function person () {};
var per = person.prototype;//Gets the prototype of the object;
var arr = per.constructor;//prototype Access object;
arr = function person () {};
ToString ();
ValueOf ();
hasOwnProperty (key); = = Identify attributes or methods that are themselves rather than inherited properties;
12.Math objects;
Math.PI =>π;
Math.sqrt2 = root 2;
Math.Abs () = absolute value;
Math.random () = Returns a random number of 0 ~ 1;
Math.max () \math.min ();
MATH.AQRT () = calculates the square root;
MATH.PSW (J, I); + = Calculates the i-th square of J;
Math.floor (); = Math.floor (1.5) + 1;
Math.ceil (); = Math.ceil (1.2) + 2;
Math.Round (); = Math.Round (1.2) + 1; Math.Round (1.5) = 2;
Math.sin () = Returns the sine of the specified number of degrees;
Math.Cos () = Returns the cosine of the specified number of degrees;
Math.atan2 () = Returns the tangent value specifying the angle of the number;
13.URL;
Method:
encodeURI (); + = encode the entire URL;
decodeURI (); = = To decode the upstairs code;
encodeURIComponent ();//encode the latter part
decodeURIComponent () = To decode the upstairs code;
14.Date objects;
Initialization
var date = new Date ();
var date = new Date (' 2014/10/01 ');
Properties and methods:
toUTCString () = Converts a Date object to a string based on Universal Time (UTC) and returns the result;
getFullYear () \setfullyear (); = = Get year \ Set year
GetMonth () \setmonth (); = Month
GetDay () \setday (); Week
GetDate () \setdate (); Days
GetHours ()/getminutes ()/getseconds ()/sethours ()/setminutes ()/setseconds (); \ \ Hours
GetTime () \settime (); \ UTC time milliseconds; from 1970/1/1 to 0:00;

JavaScript basic Syntax &2

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.