JavaScript Basics (Next day)

Source: Internet
Author: User
Tags natural logarithm pow shallow copy square root hasownproperty

Appetizer: If you copy an array (shallow copy)?

beginning of the sentence: Http://www.w3school.com.cn/js Rhino book, JS advanced Programming, Life Essentials, recommended to buy JS Rhino Book <javascript authoritative guide >

Boolean

!! Forcing type conversions

!! []//true

!! {}//true

!!‘ False '//true

Number

Number.MAX_VALUE//Reverse traversal is useful when setting the initial value

Number.min_value

(2.1234). toFixed (2)//2.12 (rounding, rounding, accuracy lost, not very accurate (quad six into 50% double)) Control decimal point 0-20

(2.444). toFixed (2); "2.44"

(2.445). toFixed (2); "2.44" exception

(2.446). toFixed (2); "2.45"

(2.4444). toFixed (3); "2.444"

(2.4445). toFixed (3); "2.445" is correct

(2.4446). toFixed (3); "2.445"

/*
* Analog math.tofixed () method-------> Thanks for the contribution of Brother
* Optimization: Rounding accuracy problem (quad six into 50% pairs)
*/
function ToFixed2 (number,fractiondigits) {
Return Math.Round (Number*math.pow (10,fractiondigits))/math.pow (10,fractiondigits);
}
Console.log (ToFixed2 (2.224,2)); 2.22
Console.log (ToFixed2 (2.225,2)); 2.23
Console.log (ToFixed2 (2.226,2)); 2.23
Console.log (ToFixed2 (2.2224,3)); 2.222
Console.log (ToFixed2 (2.2225,3)); 2.223
Console.log (ToFixed2 (2.2226,3)); 2.223

Math.PI.toPrecision (7); "3.141593" turns into a string of 10 binary form.

String

Cannot be modified after string creation

var s = ' ABCD ';

Console.log (S[1]);

S[1]= ' E ';

Console.log (S[1]);

Console.log (s);

"XX". Trim (); This is supposed to be es3.

' ABCD '. Localecompare (' ABCD '); 0

' XxX '. toLowerCase (); xxx Turn lowercase

' XxX '. toUpperCase (); XXX Turn Capital

Split (string or regular, maximum length of the array);

' ABCDEFG '. substr (start position, length); ' ABCDEFG '. substr (0); Intercept to the end

' ABCDEFG '. substring (start position, end position); ' ABCDEFG '. substring (0); Intercept to the end

var a = ' ABCD ';

var b= a.substr (0);

var C = A;

b= ' bbbb ';

c= ' CCCC ';

Console.log (a); Abcd

' ABCDEFG '. Slice (start position, end position); ' ABCDEFG '. Slice (0); Intercept to the end

' ABCDEFGC '. Search (/* regular *//c/img); First position of substring matching regexp, not found-1

Replace (Regular or string, replacement string);

var a = ' ABCDEFGC ';            A.replace (' C ', ' Z '); Abzdefgc

var a = ' ABCDEDGC ';    A.replace (/c/img, ' z '); Abzdefgz

var a = ' ABCDEDGC ';    A.replace (/c/img, ' z ');  Console.log (a); ABCDEDGC return new string, do not modify the original yo

Match (regular or string);

' ABCDEFGC '. Match (' C '); [' C ']

' ABCDEFGC '. Match (/C/IMG)//["C", "C"] returns a matching array

IndexOf (retrieve string, start position); From the go, return to the search location, not found-1

LastIndexOf (retrieve string, start position); From the back, back to the search location, not found-1

Concat (one or more strings connected);

var a = ' ABCDEFG ';

A.concat (' hijklmn ');

Console.log (a); ABCDEFG, return the new string, do not modify the original yo

CharAt (index position); return corresponding character

' ABCD '. charAt (200); "Could not find an empty string to return

charCodeAt (index position); Returns the corresponding character Unicode encoding

' ABCD '. charCodeAt (200); Nan cannot find Nan

Date

var data = new Date ();

http://blog.csdn.net/hudashi/article/details/7069600//Introduction to GMT UTC

GMT is GMT, which is the abbreviation for Greenwich Mean time.

UTC is the coordinated world time, which is coordinated universal time. It is a more accurate GMT.

GetDate () Returns the day of the one month (1 ~ 31) from the Date object.
GetDay () Returns the day of the week (0 ~ 6) from the Date object.
GetMonth () returns the month (0 ~ 11) from the Date object.
getFullYear () returns the year as a four-digit number from a Date object.
GetHours () returns the hour (0 ~ 23) of the Date object.
Getminutes () returns the minute (0 ~ 59) of the Date object.
Getseconds () returns the number of seconds (0 ~ 59) of the Date object.
Getmilliseconds () returns the milliseconds (0 ~ 999) of the Date object.
GetTime () returns the number of milliseconds since January 1, 1970.

Regexp

var r = new RegExp ("AAA", "IMG"); <==> var r =/aaa/img;

exec (characters to be tested)

/c/img.exec (' ABCDEFGC '); [' C ']

New RegExp ("C", "IMG"). EXEC (' ABCDEDGC '); [' C ']

Test (the character to be tested);

var a= ' ABCD ';

var r = new RegExp (' C ', ' img ');

var r2 =/c/img;

Console.log (R.test (a)); True

Console.log (R2.test (a)); True

Error

Evalerror: Error occurred in Eval ()
SyntaxError: syntax error, error occurs in eval (), because other points occur syntaxerror will not pass the interpreter
Rangeerror: Value out of range
Referenceerror: Reference not available
TypeError: The variable type is not expected
Urierror: Error occurred in encodeURI () or decodeURI ()

throw new error (0, "Error Demo");
Throw ({name: ' name ', message: ' message '});

Function

Apply (The specified object, array of arguments)

var a = {0: ' BBB ', 1: ' CCC ', length:2};

[].slice.apply (a); ["BBB", "CCC"]

Call (The specified object, parameter 1, parameter 2,.... Unlimited ...);

var a = {0: ' BBB ', 1: ' CCC ', length:2};

[].slice.call (a); ["BBB", "CCC"]

Here are a few easy-to-understand examples

function A (PARAM1,PARAM2) {
this.b = param1;
THIS.C = param2;
}
var obj = {};
var T1 = a.apply (obj,[' bbb1 ', ' ccc1 ']);
Console.log (obj);
var t2 = a.call (obj, ' bbb2 ', ' ccc2 ');
Console.log (obj);

Arguments.callee; Bad performance, not recommended

function A (num) {
if (num) {
Console.log (Arguments.callee () + "come in");
}else{
Return "TM";
}
}
A (3); TM come in

Closed Package

function A () {
var v = ' VVV ';
function f () {
return v;
}
return f ();
}
Console.log (A ()); Vvv

Array

Delete

var a =[1,2,3]; Delete A[1]; A [1, UNDEFINEDX1, 3]

Pop (); Delete the trailing element and return the deleted element without deleting the return undefined

var a = []; A.pop (); A //[];

Push (); add one or more trailing elements, and return length,

Shift (); Used to remove the first element from the array and return the value of the first element.

var a = []; A.shift (); Undefined

Unshift (); Adds the beginning one or more elements and returns the length of the

Reverse (); The order of the elements in the array, change the original array?

Sort (); Sort, change the original array?

[1,3,2,6,5,4].sort (); [1, 2, 3, 4, 5, 6]

[' 1 ', ' 3 ', ' 2 ', ' One ', ' n '].sort (); ["1", "11", "12", "2", "3"];

[' 1 ', ' 3 ', ' 2 ', ' One ', ' N '].sort (A, B) {return-A-b}); ["1", "2", "3", "11", "12"]

Concat (); Concatenate two or more arrays and return a new array, changing the original array?

var a = [n/a]; b=[4,5,6]; A.concat (b); A

var a = [n/a]; b=[4,5,6]; var c = a.concat (b,b,b); C

Join (); Put all the elements of the array into a string. element is delimited by the specified delimiter.

Slice (start position, end position); Returns the selected element from an existing array

var a = [n/a]; var b= a.slice (); Console.log (b);

Splice (Delete the location, delete the quantity, replace the new item can be multiple); The return value is an array of elements that are deleted,!!! Key methods, increase/delete/change all live.!!!! Change the original array?

Increase

var a = [4,5,6];  A.splice (2,0, ' X ');  A Added in front of delete location

By deleting

var a = [4,5,6]; A.splice (2,1); A

Change

var a = [4,5,6]; A.splice (2,1, ' X '); A

Object

All objects are inherited from the Object.prototype

typeof null = = = ' object '//true

hasOwnProperty (); Determines whether the property is the current object

var a = {b: ' BBB ', C: ' CCC '};
var B = function () {
This.d= ' DDD ';
};
B.prototype = A;
var bb = new B ();
Console.log (Bb.hasownproperty (' B ')); False
Console.log (Bb.hasownproperty (' d ')); True

for (var i in BB) {
if (Bb.hasownproperty (i)) {
Console.log (i);
}
}

isPrototypeOf (); Determine if an instance is on a prototype chain

var s = ' DD ';         String.prototype.isPrototypeOf (s); False

var s = new String (' DD ');      String.isprototypeof (s); False

var s = new String (' DD ');      String.prototype.isPrototypeOf (s); True

propertyIsEnumerable (); Whether it is enumerable (for in)

Object.propertyisenumerable (' toString '); False

Object.propertyisenumerable (' __proto__ '); False

({A: ' A '}). propertyisenumerable (' a '); True

toLocaleString ();

New Date (). toLocaleString (); "2015/5/7 12:16:24"

ToString ();

function A () {}; A.tostring (); "Function A () {}"

ValueOf ();

New Number (2). ValueOf () = = = 2//true

//------------------------------------------------------------------------------------

Global methods

Math

Http://www.w3school.com.cn/jsref/jsref_obj_math.asp

ABS (x) The absolute value of the return number.
ACOs (x) returns the inverse cosine of the number.
ASIN (x) returns the inverse chord value of the number.
Atan (x) returns the inverse tangent of x with a value between-PI/2 and PI/2 radians.
ATAN2 (y,x) returns the angle from the x-axis to the point (X, y) (between-PI/2 and PI/2 radians).
Ceil (x) is rounded on the logarithm.
COS (x) returns the cosine of the number.
EXP (x) returns the exponent of E.
Floor (x) is rounded down with a logarithmic.
Log (x) returns the natural logarithm of the number (bottom is e).
Max (x, y) returns the highest value in X and Y.
Min (x, y) returns the lowest value in X and Y.
The POW (x, y) returns the y power of X.
Random () returns an arbitrary number between 0 and 1.
Round (x) rounds the number to the nearest integer.
Sin (x) returns the sine of the number.
SQRT (x) returns the square root of the number.
Tan (x) returns the tangent of the angle.
Tosource () returns the source code of the object.
ValueOf () returns the original value of the Math object.

Print window

decodeURI

decodeURIComponent

encodeURI

encodeURIComponent

Eval

Isfinite

IsNaN

parseint

Parsefloat

JavaScript Basics (Next day)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.