SoFunction
Updated on 2025-04-14

Basic knowledge of JavaScript objects


property
E Returns the constant e (2.718281828...).
LN2 returns the natural logarithm of 2 (ln 2).
LN10 returns the natural logarithm of 10 (ln 10).
LOG2E returns the logarithm of e with 2 as low as  (log2e).
LOG10E Returns the logarithm of e with 10 as low as 10.
PI returns π (3.1415926535...).
SQRT1_2 returns the square root of 1/2.
SQRT2 returns the square root of 2.
method
abs(x) returns the absolute value of x.
acos(x) Returns the inverse cosine value of x (the angle where the cosine value is equal to x) and is expressed in radians.
asin(x) returns the inverse sine value of x.
atan(x) returns the arctangent value of x.
atan2(x, y) returns the amplitude angle of the complex number corresponding to the point (x, y) in the complex plane, expressed in radians, and its value is between -π and π.
ceil(x) returns the smallest integer greater than or equal to x.
cos(x) returns the cosine of x.
exp(x) returns the x power (ex) of e.
floor(x) returns the maximum integer less than or equal to x.
log(x) returns the natural logarithm of x (ln x).
max(a, b) Returns the larger number in a, b.
min(a, b) Returns the smaller number in a, b.
pow(n, m) Returns the m power of n (nm).
random() returns a random number greater than 0 and less than 1.
round(x) returns the value after x rounding.
sin(x) returns the sine of x.
sqrt(x) returns the square root of x.
tan(x) Returns the tangent of x.

Date date object. This object can store any date from 0001 to 9999, and can be accurate to milliseconds (1/1000 seconds). Internally, the date object is an integer that starts from 0:00 on January 1, 1970 to the date referred to by the date object. If the date referred to is earlier than 1970, it is a negative number. All dates, if the time zone is not specified, use the "UTC" (world time) time zone, which is numerically the same as "GMT" (Greenwich Time).
Define a date object:

var d = new Date;

This method makes d a date object and has an initial value: the current time. If you want to customize the initial value, you can use:

var d = new Date(99, 10, 1);    //October 1, 99
var d = new Date('Oct 1, 1999'); //October 1, 99

Wait for methods. The best way is to use the "method" introduced below to strictly define time.

method
There are many methods like "g/set[UTC]XXX", which means that there is both a "getXXX" method and a "setXXX" method. "get" means obtaining a certain value, and "set" means setting a certain value. If it has the letter "UTC", it means that the obtained/set value is based on UTC time, and if it does not mean that it is based on local time or the default time of the browsing period.
If not specified, the usage format of the method is: "<Object>.<Method>", the same below.
g/set[UTC]FullYear() Return/set the year, expressed in four digits. If “[UTC]FullYear(99)” is used, the year is set to 0099.
g/set[UTC]Year() returns/set the year, expressed in two digits. When setting, the browser automatically adds "19" to start, so use "[UTC]Year(00)" to set the year to 1900.
g/set[UTC]Month() returns/set month.
g/set[UTC]Date() returns/set date.
g/set[UTC]Day() returns/set the week, 0 represents Sunday.
g/set[UTC]Hours() returns/set the number of hours, 24-hour time.
g/set[UTC]Minutes() returns/set the number of minutes.
g/set[UTC]Seconds() returns/sets the number of seconds.
g/set[UTC]Milliseconds() returns/set the number of milliseconds.
g/setTime() Return/set time, which is the internal processing method of the date object: the number of milliseconds to the date referred to by the date object starts from 0:00 on January 1, 1970. If you want to delay the time referred to by a certain date object by 1 hour, use: "(() + 60 *60 *1000);" (one hour 60 min, one minute 60 second, one second 1000 ms).
getTimezoneOffset() returns the number of minutes between the time zone used by the date object and the Greenwich time. In the urban area of ​​Greenwich East, this value is negative, for example: China Time Zone (GMT+0800) returns "-480".
toString() returns a string describing the date referred to by the date object. The format of this string is similar to: "Fri Jul 21 15:43:46 UTC+0800 2000".
toLocaleString() returns a string describing the date referred to by the date object, and represents the format in local time. For example: "2000-07-21 15:43:46".
toGMTString() returns a string describing the date referred to by the date object, in GMT format.
toUTCString() returns a string describing the date referred to by the date object, in UTC format.
parse() Usage: (<date object>); Returns the internal expression of the date object.



Global Object
Global objects never appear in shape. They can be said to be virtual, with the purpose of "objecting" global functions. In the Microsoft JScript language reference, it is called "Global object", but the methods and properties that reference it never use "" (and there will be an error in doing so), but directly use "xxx".

property
NaN said it early in the morning.
method
eval() runs the string in brackets as standard statements or expressions.
isFinite() returns true if the number in brackets is "finite" (between Number.MIN_VALUE and Number.MAX_VALUE); otherwise returns false.
isNaN() Return true if the value in brackets is "NaN", otherwise return false.
parseInt() returns the value after converting the contents in brackets into integers. If there is a string in parentheses, the numeric part at the beginning of the string is converted into an integer, and if it starts with a letter, it returns "NaN".
parseFloat() returns the value after converting the string in brackets to a floating point number. The numerical part at the beginning of the string is converted into a floating point number. If it starts with a letter, it returns "NaN".
toString() Usage: <Object>.toString(); convert the object into a string. If a value is specified in parentheses, all values ​​are converted to a specific binary during the conversion process.
escape() returns the encoded new string in parentheses. This encoding is applied to the URL, which means writing spaces into the format "%20". "+" is not encoded. If "+" is also encoded, please use: escape('...', 1).
unescape() is the counter-process of escape(). Uncode the string in brackets and becomes a general string.


Function definition
The so-called "function" is an object or method of an object with a return value.
Types of functions
Common functions include: constructors, such as Array(), which can construct an array; global functions, that is, methods in global objects; custom functions; and so on.
Custom functions
Defining a function uses the following statement:

function function function name ([parameter set]) {
    ...
[return[ <value>];]
    ...


Among them, braces used after function and at the end of the function cannot be omitted, even if the entire function only has one sentence.
Function names and variable names have the same naming regulations, that is, they only contain alphanumeric underscores, letter rows, and cannot be repeated with reserved words.
Parameter sets are optional, but there must be brackets.
Parameters are the bridge for passing information outside the function to the function inside. For example, if you want to ask a function to return a cube of 3, you have to let the function know the value of "3". At this time, there must be a variable to receive the value, which is called a parameter.
A parameter set is a collection of one or more commas separated by commas, such as: a, b, c.
There are at least one line statement inside the function, which are not executed immediately, but only when other programs call it. These statements may contain a "return" statement. When executing a function, when encountering a return statement, the function immediately stops execution and returns to the program that called it. If "return" is followed by <value>, the value is returned while exiting the function.
Inside the function, parameters can be used directly as variables, and some new variables can be created using the var statement, but none of these variables can be called by procedures outside the function. To enable the information inside the function to be called externally, either use "return" to return the value or use a global variable.
Global variables The variable defined in the "var" statement in the "root" (non-internal function) of Script is a global variable, which can be called and changed anywhere in the entire process.
Example

function addAll(a, b, c) {
    return a + b + c;
}

var total = addAll(3, 4, 5); 

This example creates a function called "addAll", which has 3 parameters: a, b, and c, which is to return the result of adding three numbers. Outside the function, use "var total = addAll(3, 4, 5);" to receive the return value of the function.
Most of the time, functions do not return values. This kind of function is called "procedure" in some languages ​​that emphasize strictness, such as "Sub" in Basic and "procedure" in Pascal.

Attributes
arguments A array that reflects the parameters specified when an external program calls a function. Usage: Call "arguments" directly inside the function.