Built-in objects (arrays, Boolean, number, strings) and custom objects in JavaScript

Source: Internet
Author: User
Tags first string

The built-in objects commonly used in JS are: Arrays, Boolean classes, number classes, strings. The following describes the respective common methods

The array in JS

1. Declaration of an array
The ① literal declaration uses the [] Declaration array directly:
var arr=[1,2, "3", true,null,undefined];

in JS, the same array can hold various data types.

② using the New keyword declaration: var arr = new Array ();
parameters can be in three different forms:
>>> does not pass parameters, which means to create an empty array of length 0;
>>> passes in a numeric value that represents the creation of an array of length. But the length of the array in JS can be changed dynamically at any time;
>>> passes in multiple values, representing the creation of an array and the values as array elements.

2. Reading and deleting elements in the array:
① uses [] to read array elements.
② The additions and deletions of elements in the array:

>>> Delete elements in an array: Delet arr[1];
>>>. Push (): Appends an element to the end of the array
>>> pop (): means to delete the last element of an array
>>> Shift (): Delete the first element of an array
>>>. Unshift (): Inserts a new element at the beginning of the array

various methods of arrays in 3.JS:
①.join ("-"): An incoming delimiter that is used to concatenate an array into a string with the specified delimiter.
If the parameter is set to NULL, the default is separated by commas.
②.concat (): Joins two or more arrays, returns a new array after the connection, and if the concatenated array is a two-dimensional array, only one layer is opened;

[1,2].concat ([3,4])-->[1,2,3,4]
[1,2].concat ([3,4,[5,6]])-->[1,2,3,4,[5,6]

③.sort (): Sorts the array.
>>> By default, the ASCII code will be sorted in ascending order;
The >>> parameter can be passed directly to the comparison function:
Arr.sort (function () {
return a-b;//Ascending
return b-a;//Descending

④.reverse (): Flips the element order of the original array.

⑤.slice (start,end): Receives two values, intercepts a sub-array, and returns.
>>> Pass in a value indicating that the current subscript starts at the end;
>>> Pass in two values indicating that the end is truncated from start, with start not containing end
>>> start and end can be negative. Positive numbers indicate left to right, starting from 0;
negative numbers indicate right-to-left, starting from 1.

⑥.indexof (): Finds the index of the first occurrence of an array element
. LastindexOf (): Finds the last occurrence of an array element subscript

⑦.foreach (); receives a callback function that iterates through the array
[This function is not available until IE8]
. ForEach (function (item,index) {
Console.log (item+ "-------" +index);

⑧.map (): array mapping, in the callback function, you can have a return value. Represents each value that will be returned, assigned to a new array.
[This function is not available until IE8]
var arr1=arr.map (function (Item,index,arr) {
return item;

Boolean class

The Boolean class is declared in two ways: the literal declaration and the New keyword declaration.

1. Use the new keyword to declare the variable, with typeof detection is the object type;

2. Use literal declaration of variables, with typeof detection is a Boolean type

If you do not use the New keyword directly as a Boolean () function, you can convert various data types to variables of type Boolean

Number class

The common methods of the number class are:

1. ToFixed (2): Converts the numeric value to a string, rounding it, preserving the decimal number of the specified digits.

2. ToString (): Convert numeric value to string

3. toLocaleString (): Converts values to a string in local format, thousands separators, and a group of three numbers.


4. ValueOf (): Takes the object type to the base numeric value.


1. The. Length property returns the lengths of a string
2. Read each character in the string:
Str[0] or Str.charat (0)
3. Other common methods:

①.tolowercase (): Convert string to lowercase
②.touppercase (): Convert string to uppercase
③.substring (INDEX1,INDEX2): Returns the string between Index1 and Index2, including the index1 corresponding character, not including the index2 corresponding character
④.indexof (): Find where a character value first appears in a string
⑤.lastindexof (): Finds the last occurrence of a specified character or substring in a string
⑥.split (""): passing in the specified character, splitting the string into multiple substrings, returning an array of type string
⑦.replace (/h/g, "*"): If the first argument is a string, you can only replace the first string, and if you want to replace more than one, use a regular expression.

js the declaration of a custom object

1. Use literal declaration:
var obj={
Name: "Zhangsan",
Func:function () {

① between attributes and property values: Separates the key-value pairs
② multiple pairs of attributes, separated by commas
The key of the ③ object, which can be a variety of data types (except array [] and object {}).
The value of the object, which can be a variety of data types

2. Declaring with the New keyword
var obj1=new Object;
Obj1.name= "Zhangsan";
Obj1.func=function () {

3. Reading and writing of object properties and methods
① use, invoke
Outside the object, you can use the Obj.age
Inside the object, you can use Obj.age or this.age

Note: In an object, using the variable name directly defaults to using a global variable. If you want to use the object's properties, you must call the object name or this
② using [key] Call: Obj[age] obj[1] obj["name"]
If the key of the object contains special characters, it cannot be used. When called, you can call with brackets.

4.obj.hasownproperty (keys) detects if a key belongs to an object

5.delete Obj.name Delete an object's properties

Built-in objects (arrays, Boolean, number, strings) and custom objects in JavaScript

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.