Some common built-in objects in JS

Source: Internet
Author: User

In JS , often encounter clearly know an object has a property or method, but do not know how to write the situation. Below, I have listed some of the properties and methods of the built-in objects commonly used in JS.

Math object:

the Math object's role is to perform common arithmetic tasks.

First , the most commonly used attribute in the Math property is PI , which we typically invoke with Math.PI . The value of output in the console is 3.141592653589793, which is pi. the commonly used properties of the Math object are PI , the other uses less, if used when the direct search of the manual is good.

And then to talk about its methods,Math built-in objects are still quite a lot of, but are relatively simple, or very easy to remember.

Math.Abs (x);//the absolute value used to return the numberMath.ceil (x);//Rounding up, such as 1.1,1.9 the two numbers returned is 2, if the incoming number is negative, such as -1.1,-1.9, the return value is-1,
At first it was easy to confuse, why not-2, at this time as long as the number of axes, it can be very clear to see that positive upward rounding is the direction of the net, then negative numbers are the same. Math.floor (x);//Rounding down, exactly the opposite of the Math.ceil () method. Math.max (x, y,...);//returns the maximum value in all the numbers in parentheses, noting that the number in parentheses can be as many as possible. math.min (x, y,...);//returns the minimum value in all the numbers in parentheses. As with Math.min (), the numbers in parentheses can be any number. Math.pow (x, y);//returns the Y power of the computed x. Which is the value multiplied by y x. Math.Round (x);//returns the result after X is rounded. math.random (x);//returns a random number from 0 to 1, note that 0 here is available, but 1 is not.

Date object:

the Date object is used to process dates and times. We can Create a new Date object through new, creating the following format:

var New Date (); // Create a Date object by means of new;

The properties of the date object do not use much, in most cases, we use his method, after the use of the var keyword defines a date , you can call its method:

Date.getfullyear ();//returns the year from the Date object (that is, the date here). date.getmonth ();//returns the month from the date object. Note: The month here is to return 0 to 11 of the number, 0 corresponds to January, 1 corresponds to February, so remember to add one when using. date.getdate ();//returns a day in one months from a Date object. Date.getday ();//returns a day in one weeks from a Date object. Note that the return is also a number starting from 0, 0 corresponds to Sunday, 1 corresponds to Monday, and 6 corresponds to Saturday. date.gethours ();//returns the hour of the Date object (0~23). date.getminutes ();//returns the minute of the Date object (0~59). date.getseconds ();//returns the number of seconds (0~59) of a Date object. date.gettime ();//returns the number of milliseconds from January 1, 1970 to the time set by the Date object. 

Array object:

  

var arr=New Array ();

As with the Date object, array creates a new object in the same way that it does. The array object has one of the most commonly used properties, which is the length used to set or return the number of elements in the array. After creating a new array object, we can directly set or return the number of elements in the array using Arr.length.

Then there are the methods in the array, and I'll list some of the methods that are commonly used in arrays.

Arr.concat (Arrayx ...)//used to concatenate two or more arrays, where arrax can be an array, or it can be a specific value, with each item separated by commas. Arr.join ();//used to return a string containing all the elements in the array, by default with a comma as the delimiter, but in parentheses you can define the style of the delimiter, for example, the vertical line is Arr.join ("|"); The delimiter is wrapped in semicolons. Arr.push ();//adds one or more elements like the end of the array and returns the length of the new array. The added elements are enclosed in parentheses and separated by commas. Note: The return value of this method is the length of the new array. arr.reverse ();//reverses the order of the elements in the array. Directly calling this method only after the function has been reversed order, direct Console.log (arr) can directly output the sorted array. Arr.sort ();//used to sort the array. If the method is called without parameters, the elements in the array are sorted alphabetically, with more precise points, sorted in the order of character encodings.
If you want to sort by other criteria, you need to provide a comparison function to define the order of the sort. arr.tostring ();//converts the array to a string and returns the result, and the returned string is separated by commas. arr.valueof ();//returns the original value of the array object, which returns the entire array.

Some common built-in objects in JS

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.