Home > Web Front-end > JS Tutorial > What are the commonly used objects in js? Detailed explanation of the properties and methods of js objects

What are the commonly used objects in js? Detailed explanation of the properties and methods of js objects

php是最好的语言
Release: 2018-08-08 15:50:13
Original
3278 people have browsed it

There are many ways to get the properties of objects in js. This article does not go into too much detail. The article focuses on explaining some common objects in js and their respective properties and methods. The article is a bit long and may look messy, but it is all The author carefully compiled it in the hope that it can be helpful to everyone and shares it with everyone as a reference. Please forgive me for any shortcomings.

1. Number object

Number object represents a numerical date, integer or floating point number. Normally, you don't need to worry about Number objects because the browser automatically converts numeric literals to instances of the Number class.

1. Create a Number object

##var val = new Number(number);

##If the parameter cannot be converted to a number , it will be returned as NaN(Not-a-Number) . 2,

Number attributes

MAX_VALUE The maximum number of possible values ​​in JavaScript can be 1.7976931348623157E 308 MIN_VALUE The smallest possible number of values ​​in JavaScript can be 5E-324 NaN NEGATIVE INFINIT
##Properties

Description

## is equivalent to a value that is not a number

##Y

Smaller than MIN-VALUE

##POSITIVE_INFINIT
Y

A value greater than MAX-VALUE

prototype is a static property of a digital object. Use the properties of the prototype object to assign new properties and methods to the digital object in the current document.

3. Numerical method

The format of the string value version of the current number returned may change depending on the browser's regional settingstoPrecision( ) defines the total number of how many to display a number (including the numbers to the left and right of the decimal point)toString() Returns the string representation of the value of the number## 2. Boolean object

##Method

Description

constructor()

Returns the function that creates an instance of this object. By default this is a numeric object

toExponentia

l()

Force a number to be displayed in exponential notation, even if the number is within the range that JavaScript normally specifies using standard notation

toFixed()

Format a number as a decimal with a specific number of digits to the right of the decimal point

##toLocaleStrin

##g()

valueOf()

Return number The value of

Boolean objects represent two values, either "true" or "false".

1. Create a Boolean object

##var val = new Boolean(value);

If the value parameter is omitted or the parameter is 0, -0, empty, false, NaN, undefined, or an empty string (" "), the initial value of this object is false.

2. Boolean attributes

##Attributes Description3,

constructor

Returns a reference to the Boolean function that created this object

##prototype

## Prototype properties allow you to add an object's properties and methods

Boolean methods

MethodDescription##toSource() toString() ##valueOf() String object

Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object.

According to the value of the object Returns "true" or "false".

Return the value of the number

##3.

The String object operates on a combination of characters (i.e., a string) through a large number of helper methods that hide the JavaScript string primitive data type.

Because JavaScript can automatically convert between original string arrays and string objects, you can call any auxiliary method of the string object to act on the original characters. String data.

1, Create a String object

##var val = new String(string);

The parameter string is a correctly encoded character sequence.

2. String attribute

##Attribute Description##length

constructor

Returns a reference to the Boolean function that created this object

##prototype

## Prototype properties allow you to add properties and methods of an object

The length of the string

3, String Object Method

##MethodDescription##Compares two strings in locale-specific ordermatch() Find one or more regular expressions Match replace() Replacement and regular Substring matched by the expression search() Retrieve values ​​that match the regular expression##slice() split() substr() substring() toLocaleLowerCase()toLocaleUpperCase()##toUpperCase()Convert the string to uppercase ##valueOf() Return the value of the number

charAt()

Returns the character at the specified position

charCodeAt()

Returns the Unicode encoding of the character at the specified position

concat()

##Connection string

indexOf()

Retrieve string

lastIndexOf()

Retrieve string from back to front

localeCompare()

Extract fragments of the string and return the extracted parts in the new string

Split the string into a string array

Extract the specified number of characters from the string from the starting index number

#Extract the characters between the two specified index numbers in the string

Convert the string to lowercase

Convert the string to uppercase

##toLowerCase()

Convert the string to lowercase

##toString()

Return string

4, HTML basic type wrapper for String

#fontsize() Create a string to be displayed using the specified size, just like using the tag italics() Creates a string that is italicized, just like using < ;i> Effect of label ##link() ##small()
Method Description

author()

Create an HTML anchor as a hypertext target

big()

Creates a string displayed in large font , just like the effect of using the tag

##blink()

Create a string flashing display, just like the effect of using the tag

#bold( )

Create a string bold display, just like the effect of using tag

fixed()

Creates a string displayed as typewriter text, just like using the tag Effect

fontcolor()

Create a string Use the specified color to display, just like the effect of using the tag

Create an HTML hyperlink to request another URL

Create a string to display in a small font size, just like using the tag

# #strike()

Creates a string that is displayed with a strikethrough, just like using the tag

sub()

Creates a string displayed as a subscript, just like using < sub> Effect of label

##sup()

Create a string displayed as a superscript, just like using the tag

4. Array object

Array object is used to store multiple values ​​in a single variable.

1. Create an Array object

# #var fruits = new Array("apple","orange","mango");

Parameters of the array Can be a set of strings or integers. When you specify a numeric argument to the array constructor, the initial length of the array is determined. The maximum length allowed for an array is 4,294,967,295.

An array can be created by simple assignment, as shown below:

var fruits = ["apple","orange","mango"];

##You can access and set the value of the elements in the array through the serial number (subscript), as follows:

• fruits [0] is the first element

• fruits[1] is the second element

• fruits[2] is the third element2,

array attribute

Properties

3, Array object method

Description
constructor

Returns a reference to the function that created the object

index

Retrieve the matching string starting from zero

input

Only found in arrays created via regular expressions

length

Set or return the number of elements in the array

##prototype

Prototype properties allow you to add properties and methods of an object

##MethodDescription##push() Adds an or to the end of the array more elements and returns the new length. ##reduce() reduceRigh##reverse() ##toSource( ) Return the source code of the object ##sort () # #splice() toString() unshift()

concat()

Concatenates two or more arrays and returns the result

every()

Apply the specified function to the array elements to judge. When And only if all return values ​​are true, return true, otherwise return false se

filter()

Create a new array. The elements in the array are the elements in the original array that satisfy the filter function return value and are empty

forEach()

Traverse the array from beginning to end and call the specified function for each element

indexOf()

Retrieve from beginning to end and return the index of the given element in the array Index

join()

Put all the elements in the array element into a string. Elements are separated by the specified delimiter

lastIndexO

f()

Retrieve from the end to the beginning and return the index of the given element in the array

##map()

Create a new array to store the return value of calling the specified function for each element in the original array

pop()

Delete and return the last element of the array

At the same time, Apply a function to two values ​​to reduce to a single value (from start to finish)

t()

Apply a function to two values ​​in the array at the same time, reducing it to a single value (from tail to head)

Reverse the order of the elements in the array

shift()

Delete and return the first element of the array

##slice()

From an existing array Returns the selected element

##some()

Apply the specified function to the array elements to judge. Only one return value is true, return true, otherwise return false

Sort the elements in the array

Insert or delete elements in the array

Convert the array to a string and return the result

Adds one or more elements to the front of the array and returns the length of the new array

5. Date object
The Date object is a built-in data type of the JavaScript language . Date objects are created using new Date() as shown below.

Once a Date object is created, many methods can be used to manipulate it. Most methods only allow getting and setting the year, month, day, hour, minute, second, and millisecond fields of the object, either in local time or Coordinated Universal Time (UTC, GMT).

The ECMAScript standard requires that a Date object be able to represent any date and time, accurate to the millisecond, within 100 million days before or after 1/1/1970. This is a range of plus or minus 273785 years, so JavaScript can represent dates and times up to the year 275755.

1. Date() constructor

##new Date()

new Date(milliseconds)

new Date(datestring)

##new Date(year,month,date[,hour,minute,second,millisecond])

Parameter description:

No Argument: Without parameters, the Date() constructor creates a date object and sets it to the current date and time.

milliseconds: When a number is passed as a parameter, it is used as an internal number representing milliseconds in the date, and as the milliseconds in the return value of the getTime() method. For example, passing the parameter 5000 creates a date object that represents 5 seconds past midnight on 1/1/1970.

  • datestring: When passing a string as a parameter, it must be a string in date form, that is, a format that can be received by the Date.parse() method.

  • 7 agrument: For the last form of the constructor given above, here is a description of each parameter:

year: Integer, representing year. For compatibility (to avoid Y2K issues) the year should be specified in its entirety; use 1998 instead of 98.

month: Integer, representing month. Starting from 0 (meaning January) to 11 (meaning December).

  1. date: Integer, representing a certain day of the month.

  2. hour: Integer, representing a certain hour of the day (24-hour format).

  3. minute: Integer representing the minute fragment of the time count.

  4. #second: Integer representing the seconds fragment of the time count.

  5. #milliseconds: Integer, representing the millisecond fragment of the time count.

2. Date attribute

3, Date method

Attribute Description

constructor

Returns a reference to the function that created the object

prototype

##Prototype properties allow you to add properties and methods of an object

##MethodDescription##getFullYear() Return the year as a four-digit number from the Date object based on local time getHours() Returns the hour of the Date object according to local time (0 ~ 23) getMilliseconds()Returns the milliseconds of the Date object (0 ~ 999) according to the local time getMinutes()Returns the minutes of the Date object (0 ~ 59) according to local time getMonth() Returns the month (1 ~ 11) from the Date object according to local time ) ##getSeconds()##getTime() ##setDate() Set Date according to local time A certain day of the month in the object (1 ~ 31) ##setFullYear()setHours() setMilliseconds()setMinutes()setMonth() setSeconds()setTime() setUTCDate()##toLocalString()According to the local time format, Convert Date object to string toLocalTimeString() Convert the time part of the Date object into a string according to the local time format ##toSource()

Date()

Returns the date and time of the day

getDate()

Returns a certain day of the month (1 ~ 31) from the Date object based on local time

##getDay()

According to local time from Date The object returns a day of the week (1 ~ 6)

Returns based on local Seconds of Date object (0 ~ 59)

Returns the number of milliseconds from January 1, 1970 to the present based on local time

##getTimezoneOffset()

Returns the minute difference between local time and Greenwich Mean Time (GMT)

getUTCDate()

Returns the day of the month (1 ~ 31) from the Date object according to universal time

getUTCDay()

Returns the day of the week (0 ~ 6) from the Date object based on universal time )

##getUTCFullYear()

##According to the universal time from Date object returns four-digit year

getUTCHours()

Returns the hour of the Date object (0 ~ 23)

##getUTCMillisecond

s()

Return the milliseconds of the Date object (0 ~ 999) according to universal time

getUTCMinutes()

Returns the minute of the Date object according to universal time (0 ~ 59)

getUTCMonth()

Returns the month (0 ~ 11) from the Date object according to universal time

getUTCSeconds()

Returns the seconds of the Date object (0 ~ 59) according to world time

##getYear()

Deprecated, returns the specified Dates are based on local time. Use getFullYear() instead.

Set the year in the Date object (four digits) according to local time

Set the hour in the Date object according to local time (0 ~ 23)

Set the milliseconds in the Date object (0 ~ 999) according to local time

Set the minutes in the Date object (0 ~ 59) according to local time

Set the month in the Date object according to local time (0 ~ 11)

Set the seconds in the Date object according to local time (0 ~ 59)

Set the Date object in milliseconds according to local time

Set the day of the month in the Date object (1 ~ 31) according to universal time

setUTCFulYear()

Set the year (four digits) in the Date object according to universal time

setUTCHours()

Set the hour (0 ~ 23) in the Date object according to the universal time

setUTCMillisecond

s()

Set the milliseconds in the Date object (0 ~ 999) according to the world time

setUTCMinutes()

Set the minute in the Date object (0 ~ 59) according to universal time

setUTCMonth()

Set the month in the Date object (0 ~ 11) according to universal time

setUTCSeconds()

Set the seconds in the Date object (0 ~ 59) according to world time

setYear()

Deprecated, set to the specified date according to the local time. Use setFullYear() instead.

toDateString()

Put the date of the Date object Partially converted to a string

toGMTString()

Deprecated, converts the date to a string, using the Internet GMT convention. Use toUTCString() instead.

##toLocalDateString()

##According to the local time format, Convert the date part of the Date object to a string

toLocalFormat()

Convert the date to a string, using the format string.

Returns a string representing the source code of an equivalent date object. You can use this value to create a new object

toString()

Convert Date object to string

toTimeString()

Convert the time part of the Date object into a string

toUCTString()

Convert the Date object to a string according to world time

valueOf()

Returns the original value of the Date object

##Date static method

MethodDescription6.
##Date.parse()

Return the number of milliseconds from midnight on January 1, 1970 to the specified date (string)

Date.UTC( )

##Returns the number of milliseconds from January 1, 1970 to the specified date according to universal time

Arithmetic object

The Math object provides functions for mathematical constants properties, methods and functions.

#Unlike other global objects, Math is not a constructor. All properties and methods of Math are static, and you can call all its properties and methods by using Math as an object without creating it.

# Therefore, you can define the constant pi as Math.PI, or you can call the sin function Math.sin(x), where x is the parameter of the method.

1. Simple use

##var pi_val = Math.PI;

var sine_val = Math.sin(30);

2, Math attribute

##AttributeDescription##LOG10E Return the logarithm of base 10 (approximately equal to 0.434) ##PI ##SQRT1_2 ##SQRT2

E

Returns the arithmetic constant e, which is the base of the natural logarithm (approximately equal to 2.718)

LN2

Returns the natural logarithm of 2 (approximately equal to 0.693)

##LN10

Returns the natural logarithm of 10 (approx. Equal to 2.302)

LOG2E

returns 2 as Logarithm of the base (approximately equal to 1.414)

Return pi (approximately equal to 3.14159)

Returns the reciprocal of the square root of 2 (approximately equal to 0.707)

Returns the square root of 2 (approximately equal to 1.414)

3, Math method

##MethodDescription## is between -PI/2 and PI/2 A value in radians to return the arctangent of x ##atan2() ceil() cos() exp() floor() log() max() min() pow()

abs()

## Returns the absolute value of the number

acos()

Returns the arc cosine of the number

asin()

Returns the arcsine of the number

atan()

Returns the angle from the x-axis to the point (x,y) (between -PI/2 and PI/2 radians)

Round up the number

Returns the cosine of the number

Returns the exponent of e

Round down the logarithm

Returns the natural logarithm of the number (base is e)

Return the highest value between x and y

##Return the lowest value between x and y

Returns x raised to the y power

##random()

Returns between 0~1 Random number

round()

Round the number is the nearest integer

sin()

Returns the sine of the number

sqort()

Returns the square root of the number

tan()

Returns the tangent of the angle

toSource()

##Returns the source of the object Code

Related recommendations:

JS basics document object in DOM Detailed explanation of commonly used attribute methods

JS method to dynamically add attributes to objects

The above is the detailed content of What are the commonly used objects in js? Detailed explanation of the properties and methods of js objects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template