Home  >  Article  >  Web Front-end  >  JavaScript study notes (8) js built-in objects_basic knowledge

JavaScript study notes (8) js built-in objects_basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:52:26880browse

1. URI methods

encodeURI() and encodeURIComponent() encode URI
encodeURI() will not encode special characters that are URIs themselves, such as colon, forward slash, hello, tic-tac-toe etc.
encodeURIComponent() will encode any non-standard characters

2.eval() method: interpret the code string in the parameter

Copy the code The code is as follows:

var msg = "hello world";
eval("alert(msg)"); //"hello world"


3.Math object
Math.E The value of e in mathematics
Math.PI The value of π
Math.SQRT2 The square root of 2
Math .abs(num) The absolute value of num
Math.exp(num) e raised to the num power
Math.log(num) The natural logarithm of num
Math.pow(num,n) num nth power
Math.sqrt(num) square root of num
Math.acos(x) arccosine of x
Math.asin(x) arcsine of x
Math.atan( x) Arctangent of x
Math.atan2(y,x) Arctangent of y/x
Math.cos(x) Cosine of x
Math.sin(x) Sine of x Value
Math.tan(x) Tangent value of x

4.min() and max() methods
Copy code The code is as follows:

var max = Math.max(3,45,67,32);
alert(max); //67
var min = Math.min(2,46,74);
alert(min); //2

5. Method of rounding decimals to integers
Math.ceil() round up Round
Math.floor() Round down
Math.round() Round
Copy code The code is as follows :

alert(Math.ceil(25.1)); //26
alert(Math.ceil(25.5)); //26
alert(Math.ceil(25.9) ); //26

alert(Math.round(25.1)); //25
alert(Math.round(25.5)); //26
alert(Math.round(25.9 )); //26

alert(Math.floor(25.1)); //25
alert(Math.floor(25.5)); //25
alert(Math.floor( 25.9)); //25

6. The random() method returns a random number between 0 and 1, excluding 0 and 1
Pick a random number within a certain range Formula:
Random number = Math.floor(Math.random * total first value) // Total number = second value - first value
Copy the code The code is as follows:

//Get a random number function within the range
function selectFrom(lowerValue,upperValue) {
var count = upperValue - lowerValue 1;
return Math.floor(Math.random() * count lowerValue);
}

var num = selectFrom(2,10);
alert(num); / /A number between 2 and 10 (including 2 and 10)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn