Functions and objects

Source: Internet
Author: User
Tags date now maximum math

Last time we talked about functions, there are some things to be aware of, where the return value of this block is important, you have to figure out what is going on, each function has a default value, the return value we can probably be divided into these categories.

A: When there is no return, the function actually has a return value, and his return value defaults to undefined.

Second: If there is a return, but the value is not followed, then his return value is also the default value of undefined.

Three: If there is a return, he has a value behind him, then his return value is the value behind the band.

Also, when using return, note that when the function executes the return, it jumps out of the function, does not execute the program after the return, and all returns cannot be added or executed, and it is better to have the function always have a return value or never return a value. If a function wants to return multiple values, it is best to put them in arrays and objects, and not write multiple return.

There are also four forms of functions:

A: No parameter no return value, he has a lot of functions, generally used for code reuse.

Two: No parameter has a return value,

Three: There is no return value, the function does not return, then he has no real return value, only a default undefined.

Four: There is a return value, he has the most useful, can be seen everywhere.

Remember that there is no overload in the JS function (overload is two identical function names, but parameters, function body), in JS when there are two identical function names, different parameters, function body, the next one will be the front one overlay, all without overloading.

In JS there is no block-level scope, it has only global scope and local scope, what is the global scope? What is a local scope?

Global scope: Is the global scope that can be accessed throughout the script.

Local scope: The function is a local scope, the function of the variable can only be accessed by the function, outside the function is not accessible, but the function if the variable before the Var variable is also a global variable. This is a special thing to remember.

The expression of functions and the Declaration of functions, we want to know the difference between them:

The same point: their role is the same, all are called functions.

Different points: (1) There is no function name in the function expression, and the variable can be used as its function name because it is an anonymous function.

(2) When pre-parsing, the function declaration is preceded by the scope, and the two function expressions cannot advance to the scope, he will only advance the variable to the scope.

(3) Functions in a function declaration can then be declared money calls, functions in two function expressions can only be called after the function expression.

There is also a self-executing function, which is a function that has just been defined and executes immediately.

I know the function of the content is probably this, there may be some deep things, I do not understand, I will continue to understand, not progress is tantamount to retrogression.

Today I used the built-in object, that is, the built-in object. Built-in objects are the objects that the system defines for us. I used the Math,date,arey. These are actually not difficult, are ready-made, we come directly to use, I usually use the column:

The object of Math

One: Rounding up Math.ceil ()

Two: Rounding down Math.floor ()

Three: Rounding Math.Round ()

Four: Take 0 to 1 random number, 1 is not marry Math.random ()

V: A's B-square math.pow (A, A, a)

Six: Take maximum Math.max ()

Seven: Take the minimum value math.min ()

Date Object

Get date and time, foreign and domestic different.

General use abroad: date.todatestring and Date.totimestring

Domestic general use: Date.tolocaldatestring and date.tolocaltimestring

Date conversion, it can be accepted in three forms: (2015,10,1) (2015-10-1) (13 digits)

Date.parse is to convert a date to a 13-digit number.

There are generally two ways to get the current time:

Var Date=date Now () This is generally used in HTML5.

Var date=+new Date () This is more commonly used

There are two ways to format dates: toString valueOf

Gets the specified part of the date:

GetTime () and valueof as a result

Getmillisecond ()

Getsecond () returns a number from 0 to 59

Getminutes () returns a number from 0 to 59

GetHours () returns a number from 0 to 23

GetDate () returns the day ordinal of the previous month, the date of the month

GetDay () returns the number of weeks, starting from 0

GetMonth () returns the month, starting from 0

Getfullyeall () returns a four-digit year

Arrey Object

Conversion of arrays:

ToString () converts the array to a string, with each item separated by commas

ValueOf () returns the array object itself

Manipulating arrays:

Join () Links elements in an array into strings

Concat () Link two arrays

Slice () intercepts a new array from the array, with star starting at 0 and end starting at 1.

Splice (A,b,c) A is from the beginning of the first, B is to delete a few, C is replaced by the item.

Append and delete items

Push () added to the last face of the array

Pop () deletes the last one in the array

Shift () deletes the first of the array

Unshift () added to the front of the array

Location method

IndexOf ()

LastIndexOf ()

Array sorting

Reverse () Reverses the array, not returning a copy, but manipulating the array itself.

Sort () is an array that is also arranged by character size, from small.

Three ways to empty an array:

Array.splice (0,array.length) deletes all items in the array.

Array.length=0

Array=[]

Functions and 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.