JS function, array, date

Source: Internet
Author: User
Tags script tag

function is a key point of JS, function is a code reuse of a mechanism or code to encapsulate the function of the code snippet, function a total of two ways to define
function declaration defines a function in the same way
Functions + function Name: verbs commonly used in words, such as Getmax ... + parameter list: This is usually a formal parameter or a function) {
Function Body: Executable code
The function can be followed by a return, (return followed by a value that needs to be returned, if there is no write return or no write back value, then the function returns undefined, when there is no return, the function is just to execute the code, Do not add code below the return statement because it will not execute)
}
The recommended practice is to have the function always return a value, or not to return, and if more than one value is returned, it can be placed in an array or object.
Anonymous functions: This unnamed function, like the assignment operator, is called an anonymous function. It is generally used when binding events.
var + variable: For example a,b,c ... =function (parameter) {
function body;
Return ... ;
}
The common denominator of the two functions is that both can execute the call
The difference is: function declaration function is called by the function name, JS has pre-parsing function, pre-parsing function declaration function can be promoted to the front of the scope
The function expression does not have a name, it needs to be used when the variable is called, when JS Pre-parsing, the declaration of the variable (just declare). The Declaration of the function. The parameter prompts to the front of the scope, which causes the function expression to not run.
In JS, there is no block-level scope, only the global scope and the local scope, the global scope is the same script tag between the area, all variables can be used anywhere in the scope, the function is not declared with VAR is a global variable. The local scope refers to the area in a function.

For shorthand, there is a self-executing function:
Example: var sum =function (A, b) {
return a+b;
}
Its self-executing function is
(Function (A, b) {
Return a+b;
}( ));
or (The Function (A, b) {
Return a+b;
})( );
At the back of the function;
There is also a callback function, which is to write the function in the argument to reference.
JS data types are divided into two types, one is a simple data type, such as number, String, Boolean, null, Undefined, where the boolean total of two data types, null and undefined is a more special type, only one.
The second is the complex data type, which refers to the object, which is now exposed to an array, date,function
Developers also develop some of the more special functions, math is one, the use of math is the function of extraction, commonly used to have
Math.pow (x, y);
Math.Round (rounded, fractional);
Math.ceil (rounding up, decimal);
Math.floor (rounded down);
Math.max (x, Y, z ...);
Math.min (x, Y, z ...);
Math.random (); Generates a random number of [0,1], 1 is not taken
These are some of the functions, and the use of date dates is described below.
Var date=new date (); name and array a bit like
Console.log (date); =console.log (date.tostring); The first method calls the ToString internally by default
Console.log (Date.valueof ()); a 13-digit number is printed, referring to milliseconds, 1000 milliseconds = 1 seconds, starting from January 1, 1970 00:00 00 seconds 000 milliseconds
The foreigner generally uses the date.todatestring to represent the date, uses the totimestring to represent the day time; we also have another method in China, date.tolocaldatestring represents the date, tolocaltimestring represents the time, so the time to print out and date more closely with the Chinese aesthetic
var date = new Date (2016,6,31); How foreigners are calculated, the month is starting from 0
var date = new Date ("2016-07-31");
var date = Date.parse ("2016-07-32"); Analytical

var date = Date.now (); TTML5 method, IE9 after the browser only support
var date = +new date (); Most widely used, supported by browsers
Console.log (date);
Var date=new Date (2016,8,1);
Three parameters are acceptable:
2016,8,1 each part of the date this is the calculation method of the foreigner, the month starting from 0
Date format for "2016-8-1" string
A 13-bit number that represents the millisecond of a date
If it is not the above three ways, then return Nan

Date formatting
GetTime () returns the same number of milliseconds as the valueof () result
Getmilliseconds ()
Getseconds () returns 0-59
Getminutes () returns 0-59
GetHours () returns 0-23
GetDay () returns day of the week 0 Sunday 6 weeks 6 0--6
GetDate () returns the day ordinal of the current month, the date of the month
GetMonth () returns the month, starting from 0
getFullYear () returns a 4-bit year, such as 2016

The third part is an array, which
Two ways to create an array:
Var arr=[];
Var Arr=new array[];
. length indicates the length of the array, for example: Var arr=["A", "B", "C", "D"];
Length is 4,arr[0]= "a", arr[1]= "B"
Convert array: Converts an array to a string. toString
Returns the Array object itself. ValueOf ()
Operation Array: Join () parentheses can be used to write what separates arrays, for example: Join ("*")
Concat For example: Var arr=[12,23]; var arr1=[34,56];
Var Arr2=arr.concat (ARR1); Console.log (ARR2);
Arr2=[12,23,34,56];arr and arr1 do not change

Append the array to delete the preceding latter
Push () is added to the array's
Pop () deletes the last item of an array
Shift () deletes the first item of an array
Unshift () added to the front of the array

JS function, array, date

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.