function in JS, Date object, Math object and Array object

Source: Internet
Author: User
Tags pow

A function is a set of statements to complete a function, the function of JS is defined by the keyword function + function name + a set of parameters; functions can be called repeatedly after they are defined, often written as a function, using functions that enable the organization of code
Structure more clearly.

Its syntax structure is

function Funname (arg0, arg1, ... ArgN) {
Statements
}

function Say_hello (name, msg) {alert ("Hello" + name + ":" + msg);} Say_hello ("David", "How is you today?");

There are two ways of function in JS: 1 declaration function; 2 function expression

The above example uses a declarative function, which is a function that begins with the keyword function, which first specifies the function name, the Say_hello in the above example, and invokes the function directly using the function name plus ().

function expressions generally do not have a function name, which is usually an anonymous function of an event, such as

Document.onclick=function (str) {  alert (str);   }

The return statement is often used in functions, and it is important to note when using return

1. Any code that is behind the return statement will never be executed!
2. In JavaScript, you don't have to specify a return value!
3. When defining the parameters of a function, if the function is called without passing arguments, it does not give an error, but it is possible to determine in the function whether there is a pass-in parameter.

There should also be some understanding of global variables and local variables when using functions:
Global variables: can be referenced anywhere in the script, once you declare a global variable in a script, you can reference it anywhere in the script (including inside the function), the scope of the global variable is the entire script;
Local variable: only exists inside the function that declares to it, cannot use it outside the function, the scope of local variable is limited to inside function;
For example:

function Square (num) {var total;total = num * Num;return total;} var total = 50;alert (total), var number = square (); alert (total);

There are several intrinsic functions in JS:

1.eval takes a parameter of a string type, executes the string as code in the context, and returns the result of the execution;
2.parseInt and parsefloat Convert a string to a number that converts to an integer, which is a floating-point type
3.escape and unescape URL encoding and decoding, which is typically used when passing URLs to the server.
JavaScript is an object-oriented language, so you can use object-oriented thinking to do JavaScript programming. An object is a data entity that is composed of some properties and methods that are related to each other.

The 1.Date Date object is used to process the date and time.
Such as

var mydate = new Date (); Mydate.getfullyear ();    Get the full year (4-bit, 1970-????) Mydate.getmonth ();       Get the current month (0-11, 0 for January) mydate.getdate ();        Get current day (1-31) mydate.getday ();         Get Current week x (0-6, 0 for Sunday) mydate.gettime ();        Gets the current time (the number of milliseconds starting in 1970.1.1) mydate.gethours ();       Gets the current number of hours (0-23) mydate.getminutes ();     Gets the current number of minutes (0-59) mydate.getseconds ();     Gets the current number of seconds (0-59) mydate.getmilliseconds ();    Gets the current number of milliseconds (0-999) mydate.tolocaledatestring ();     Gets the current date var mytime=mydate.tolocaletimestring ();     Gets the current time mydate.tolocalestring ();        Get Date and time

2.Math objects

The Math object is used to handle complex mathematical operations the Math object is a global object of JavaScript and does not need to be created with new.

The Math object has several methods:
Math.Abs (-2); The method can return the absolute value of a number.
Math.Round (5.5); This method rounds a number to the nearest integer.
Math.random (); The method can return a random number between 0 (inclusive) ~ 1 (not included).
Math.ceil (1.4); The Ceil () method returns the smallest integer greater than or equal to X. (Take the big whole)
Math.floor (1.6); The floor () method returns the largest integer less than or equal to X. (Take small whole)
Math.pow (4,3);  The POW () method returns the Y power of X. (4 of the 3-second party)
Cases:

var luckey=math.round (3*math.random ()); var str= "Thank you for your participation";      if (luckey==1) {          str= "Maldives seven 7th Tour";       }        str+= "Your number is:" +luckey;        alert (str);

Array Object

The array object is a very important object in JS. The 3 ways to define an array are
var week = new Array ();
var week = new Array (' Monday ', ' Tuesday, ' Wednesday ');
var week = [' Monday ', ' Tuesday, ' Wednesday '];
Arrays have the Length property, which represents the number of elements in the array. such as Week.length. Getting an element in an array requires [], such as Week[2], which represents the element with the ordinal 2 in the array, because the sequence number of the elements in the array starts at 0, so the ordinal 2 represents the third element in the array.

var arr=["Heading 1", "Title II", "title three", "title Four", "title Five", "Title VI", "Title VII"];    Alert (arr[3])//title Four

The methods for adding elements in an array are

1.push

2.unshift

The former means adding one or more new elements to the end of the array and returning the new length of the array. The latter adds one or more new elements to the beginning of the array, and the elements in the array automatically move back, returning the new length of the array.
Cases:

var arr=["Heading 1", "Title II", "title three", "title Four", "title Five", "Title VI", "Title VII"];            Arr.push ("Come", "Let's Go");            Arr.unshift (' Look ', ' boy ') for        (Var i=0;i<arr.length;i++) {        document.write ("<br>" +arr[i]);        }

JS functions, date objects, math objects, and array objects

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.