search
HomeWeb Front-endJS TutorialJavaScript Reference Tutorial_Basic Knowledge

JavaScript Reference Tutorial
Javascript uses "object-oriented programming", or "object-oriented programming". The so-called "object programming" means dividing the scope that JavaScript can cover into large and small objects, and then continuing to divide objects below the objects until they are very detailed. All programming takes objects as the starting point and is based on objects. As small as a variable, as large as a web page document, window or even screen, they are all objects. This chapter will describe the operation of javascript "object-oriented".
Basic knowledge of objects
An object is a small piece that can be divided from the "sphere of influence" of JavaScript. It can be a piece of text, a picture, a form, etc. Each object has its own properties, methods and events. The properties of an object reflect certain specific properties of the object, such as: the length of the string, the length and width of the image, the text in the text box (Textbox), etc.; the method of the object can do some things to the object, for example, The "Submit" of the form, the "Scrolling" of the window, etc.; and the events of the object can respond to things that happen on the object. For example, submitting a form generates a "Submit event" of the form, and clicking a connection generates a "Submit event" of the form. click event". Not all objects have the above three properties, some have no events, and some only have attributes. To reference any "property" of an object, use the method ".".

Basic objects
Now we have to review what we have learned above - re-learn some data types from the perspective of objects.

Number “Number” object. This object is rarely used and the author has never seen it once. However, there are many objects belonging to "Number", that is, "variables".

Attribute

MAX_VALUE Usage: Number.MAX_VALUE; Returns "maximum value".
MIN_VALUE Usage: Number.MIN_VALUE; returns the "minimum value".
NaN usage: Number.NaN or NaN; returns "NaN". "NaN" (not a number) was introduced very early on.
NEGATIVE_INFINITY Usage: Number.NEGATIVE_INFINITY; Return: negative infinity, a value smaller than the "minimum value".
POSITIVE_INFINITY Usage: Number.POSITIVE_INFINITY; Returns: positive infinity, a value greater than the "maximum value".

Method

toString() Usage: .toString(); Return: value in string form. For example: if a == 123; then a.toString() == '123'.

String String object. The simplest, quickest, most effective, and commonly used way to declare a string object is to assign it directly.

Attribute

length Usage: .length; Returns the length of the string.

Method

charAt() Usage: .charAt(); Returns the single character at the th position of the string. Note: One character in the string is at position 0, the second character is at position 1, and the last character is at position length - 1.
charCodeAt() usage: .charCodeAt(); returns the ASCII code of a single character located at the th position of the string.
fromCharCode() usage: String.fromCharCode(a, b, c...); returns a string, the ASCII code of each character in the string is determined by a, b, c... etc.
indexOf() usage: .indexOf([, ]); this method finds from (if is given, the previous position is ignored), If it is found, its location is returned, if not found, "-1" is returned.All "positions" start from scratch.
lastIndexOf() Usage: .lastIndexOf([, ]); similar to indexOf(), but starting from the back.
split() usage: .split(); returns an array, which is separated from . determines the place of separation, and it will not contain it itself in the returned array. For example: '1&2&345&678'.split('&') returns array: 1,2,345,678. Regarding arrays, we will discuss them in a moment.
substring() usage: .substring([, ]); returns the substring of the original string, which is the original string from the position to A segment of the position's previous position. - = Returns the length of the string. If is not specified or exceeds the string length, the substring is taken from the position to the end of the original string. If a string cannot be returned at the specified location, an empty string is returned.
substr() usage: .substr([, ]); returns the substring of the original string. This string is the original string starting from the position and the length is A paragraph of . If is not specified or exceeds the string length, the substring is taken from the position to the end of the original string. If a string cannot be returned at the specified location, an empty string is returned.
toLowerCase() Usage: .toLowerCase(); returns a string in which all uppercase letters of the original string are changed to lowercase.
toUpperCase() Usage: .toUpperCase(); returns a string in which all lowercase letters of the original string are changed to uppercase.

Array array object. An array object is a collection of objects, and the objects inside can be of different types. Each member object of the array has a "subscript" used to indicate its position in the array (since it is a "position", it also starts from zero).

How to define an array:

var = new Array();

This defines an empty array. To add array elements in the future, use:

[] = ...;

Note that the square brackets here do not mean "can be omitted", the lower part of the array The mark is expressed by enclosing it in square brackets.

If you want to initialize the data directly when defining the array, please use:

var = new Array(, , ...);

For example, var myArray = new Array(1, 4.5, 'Hi'); defines an array myArray, and the elements inside are: myArray[0] == 1; myArray[1] == 4.5; myArray[2] == 'Hi'.

However, if there is only one element in the element list, and this element is a positive integer, this will define an array containing empty elements.

Note: JavaScript only has one-dimensional arrays! Never use the stupid method "Array(3,4)" to define a 4 x 5 two-dimensional array, or use the method "myArray[2,3]" to return the elements in a "two-dimensional array" . Any call of the form "myArray[...,3]" actually only returns "myArray[3]".To use a multidimensional array, use this virtual method:

var myArray = new Array(new Array(), new Array(), new Array(), ...);

In fact, this is a one-dimensional array, and each element in it is an array. When calling the elements of this "two-dimensional array": myArray[2][3] = ...;

property

length usage: .length; return: the length of the array , that is, how many elements there are in the array. It is equal to the index of the last element in the array plus one. So, if you want to add an element, just: myArray[myArray.length] = ....

Method

join() Usage: .join(); returns a string that strings together the elements in the array, using Placed between elements. This method does not affect the original contents of the array.
reverse() Usage: .reverse(); reverse the order of elements in the array. If you use this method on the array [1, 2, 3], it will change the array to: [3, 2, 1].
slice() usage: .slice([, ]); returns an array, which is a subset of the original array, starting from and ending with . If is not given, the subset is taken until the end of the original array.
sort() usage: .sort([]); arranges the elements in the array in a certain order. If is not specified, alphabetical order is used. In this case, 80 is ranked higher than 9. If is specified, the sorting method specified by will be used. It is difficult to describe, here I will only introduce some useful to you.

Sort the numbers in ascending order:

function sortMethod(a, b) {
return a - b;
}

myArray.sort(sortMethod);

Arrange the numbers in descending order: Change "a - b" above to "b - a".

For functions, please see below.

Math "Math" object, providing mathematical calculations on data. For the properties and methods mentioned below, the "usage" will not be explained in detail. Please remember to use the format "Math." when using them.

Attribute

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 the lowest value (log2e).
LOG10E returns the logarithm of e with 10 as the lowest value (log10e).
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 of x (the cosine is equal to the angle of x), expressed in radians.
asin(x) returns the arcsine of x.
atan(x) returns the arctangent of x.
atan2(x, y) returns the argument 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 e raised to the x power (ex).
floor(x) returns the largest 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 of a, b.
pow(n, m) returns n raised to the m power (nm).
random() returns a random number greater than 0 and less than 1.
round(x) returns the rounded value of x.
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 of a second). Internally, a date object is an integer that is the number of milliseconds since midnight, January 1, 1970, to the date pointed to by the date object. If the referenced date is earlier than 1970, it is a negative number. All dates and times, if no time zone is specified, use the "UTC" (Universal Time) time zone, which is numerically the same as "GMT" (Greenwich Mean 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, 1999
var d = new Date('Oct 1, 1999'); // October 1, 1999

and other methods. The best way is to strictly define time using the "method" introduced below.

Methods

There are many methods like "g/set[UTC]XXX" below, which means there are both "getXXX" method and "setXXX" method. "Get" is to get a certain value, and "set" is to set a certain value. If there is a "UTC" letter, it means that the value obtained/set is based on UTC time, if not, it means that it is based on local time or the default time of the browsing period.

If there is no explanation, the usage format of the method is: ".", the same below.

g/set[UTC]FullYear() Returns/sets the year, represented by four digits. If "x.set[UTC]FullYear(99)" is used, the year is set to 0099.
g/set[UTC]Year() Returns/sets the year, represented by two digits. When setting, the browser automatically adds "19" at the beginning, so use "x.set[UTC]Year(00)" to set the year to 1900.
g/set[UTC]Month() returns/sets the month.
g/set[UTC]Date() returns/sets the date.
g/set[UTC]Day() returns/sets the day of the week, 0 means Sunday.
g/set[UTC]Hours() returns/sets the hours in 24-hour format.
g/set[UTC]Minutes() returns/sets the minutes.
g/set[UTC]Seconds() returns/sets the number of seconds.
g/set[UTC]Milliseconds() returns/sets the number of milliseconds.
g/setTime() returns/sets the time, which is the internal processing method of the date object: the number of milliseconds calculated from 0:00 on January 1, 1970 to the date pointed to by the date object. If you want to delay the time pointed by a date object by 1 hour, use: "x.setTime(x.getTime() 60 * 60 * 1000);" (one hour 60 minutes, one minute 60 seconds, one second 1000 milliseconds ).
getTimezoneOffset() returns the number of minutes difference between the time zone used by the date object and Greenwich Mean Time. In urban areas east of Greenwich, the value is negative, for example: China time zone (GMT 0800) returns "-480".
toString() returns a string describing the date pointed 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 pointed to by the date object, expressed in local time format. For example: "2000-07-21 15:43:46".
toGMTString() returns a string describing the date pointed to by the date object, in GMT format.
toUTCString() returns a string describing the date pointed to by the date object, in UTC format.
parse() Usage: Date.parse(); Returns the internal expression of the date object.

Global object
The global object never appears. It can be said to be virtual, with the purpose of "objectifying" the global function. In the Microsoft JScript Language Reference, it is called "Global Object", but its methods and properties are never referenced with "Global.xxx" (and doing so will cause an error), but directly with "xxx".

Attribute

NaN I said it early. The

method

eval() operates on the string enclosed in parentheses as a standard statement or expression.
isFinite() returns true if the number in the brackets is "finite" (between Number.MIN_VALUE and Number.MAX_VALUE); otherwise it returns false.
isNaN() returns true if the value in the brackets is "NaN" otherwise returns false.
parseInt() returns the value after converting the content in parentheses into an integer. If the brackets are a string, the numeric part at the beginning of the string is converted to an integer, if it starts with a letter, "NaN" is returned.
parseFloat() returns the value after converting the string in parentheses into a floating point number. The number at the beginning of the string is converted into a floating point number. If it starts with a letter, "NaN" is returned.
toString() Usage: .toString(); Convert the object into a string. If you specify a value in parentheses, all values ​​are converted to the specific base during the conversion process.
escape() returns the new string encoded by the string in the brackets. This encoding is applied to the URL, that is, spaces are written in the format of " ". " " is not encoded. If you want " " to be encoded, please use: escape('...', 1).
unescape() is the reverse process of escape(). Decode the string in parentheses into a normal string.

Function
Definition of function

The so-called "function" is an object or method of an object that returns a value.

Types of functions

Common functions include: constructors, such as Array(), which can construct an array; global functions, which are methods in global objects; custom functions; etc.

Custom function

Use the following statement to define a function:

function function name ([parameter set]) {
...
[return[ ];]
...
}

Among them, the braces used after function and at the end of the function cannot be omitted, even if the entire function has only one sentence.

Function names have the same naming rules as variable names, that is, they only contain alphanumeric underscores, leading letters, and cannot repeat reserved words.

The parameter set is optional, but the parentheses must be present.

Parameters are the bridge that transmits information from the outside of the function to the inside of the function. For example, if you want a function to return the cube of 3, you have to let the function know the value "3". At this time, you need a variable to receive it. Numerical variables are called parameters.

The parameter set is a set of one or more parameters separated by commas, such as: a, b, c.

There are one or more lines of statements inside the function. These statements will not be executed immediately, but will only be executed when other programs call it. These statements may contain "return" statements. When executing a function, when a return statement is encountered, the function immediately stops execution and returns to the program that called it. If "return" is followed by , the value will be returned when exiting the function.

Within the function, parameters can be used directly as variables, and var statements can be used to create new variables, but these variables cannot be called by procedures outside the function. To make the information inside the function available for external calls, either use a "return" return value or use a global variable.

Global variables The variables defined by the "var" statement at the "root" (not inside the function) of Script are global variables, 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, c. Its function 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.

More often, functions have no return value. This kind of function is called "procedure" in some languages ​​that emphasize strictness, such as "Sub" in Basic language and "procedure" in Pascal language. ".

Attributes

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

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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool