Home  >  Article  >  Web Front-end  >  JavaScript local objects

JavaScript local objects

jacklove
jackloveOriginal
2018-06-15 15:28:021575browse

JavaScript Object

Javascript objects include: JS Array (array object), JS Boolean (Boolean object), JS Date (date object), JS Math (Math object), JS Number (numeric object), JS String (string object), JS RegExp (regular expression), JS Function (function object), Window and Screen in Browser, etc.

JS Array object

The Array object is used to store multiple values ​​in a single variable.
##contant() Concatenates two or more arrays and returns the result. join()Put all elements of the array into a string. Elements are separated by the specified delimiter. pop()Deletes and returns the last element of the arraypush() Adds one or more elements to the end of an array and returns the new length. reverse()Reverse the order of elements in the array. shift()Delete and return the first element of the arrayslice()Return selected elements from an existing arraysort() Sort the elements of the arraytoSource()Returns the source code of the object. toString()Convert the array to a string and return the result. toLocaleString()Convert the array to a local array and return the result. unshift()Adds one or more elements to the beginning of the array and returns the new length. valueOf()Return the original value of the array object
Method Description
Usage of main methods
Connect the parameters in concat() to array a

<script type="text/javascript">var a = [1,2,3];
document.write(a.concat(4,5));</script>

Output:

1,2,3,4,5

Join() creates an array and puts all its elements into a string:

<script type="text/javascript">var arr = new Array(3)
arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr.join())</script>

Output:

George,John,Thomas

sort() will create an array to sort

<script>var x=[4,5,7,1,6,9,3,10,132,12];
console.log(x.sort());
console.log(x.sort(function(a,b){
    return a-b;
}));
console.log(x.sort(function(a,b){
    return b-a;
})) ;
console.log(x);</script>

Output:

[1, 10, 12, 132, 3, 4, 5, 6, 7, 9]
[1, 3, 4, 5, 6, 7, 9, 10, 12, 132]
[132, 12, 10, 9, 7, 6, 5, 4, 3, 1]
[132, 12, 10, 9, 7, 6, 5, 4, 3, 1]

pop() creates an array and then deletes the array the last element of . Note that this will also change the length of the array:

<script type="text/javascript">var arr = new Array(3)
arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr)
document.write("<br />")
document.write(arr.pop())
document.write("<br />")
document.write(arr)</script>

Output:

George,John,Thomas
Thomas
George,John

push() method adds one or more elements to the end of the array and returns the new length.

<script type="text/javascript">var arr = new Array(3)
arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr + "<br />")
document.write(arr.push("James") + "<br />")
document.write(arr)</script>

Output:

George,John,Thomas
4
George,John,Thomas,James

This article explains the local objects of JavaScript. For more related content, please pay attention to the php Chinese website.

Related recommendations:

Introducing a small case for getting started with JS

Achieving dynamic display of processes through js

particlesJS usage introduction related content

The above is the detailed content of JavaScript local objects. For more information, please follow other related articles on the PHP Chinese website!

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