[2] JavaScript foundation # --2019-08-17 00:26:15

Original: http://blog.gqylpy.com/gqy/238

"


table of Contents

# Functions

1. Definitions

2. arguments parameters

3. Global variables and local variables

4. parsing

# Built-in objects and methods

1. Custom Objects

2. Class of inheritance

3. Date

4. JSON

5. RegExp

6. Math


# Functions

1. Definitions

  1. The basic functions:
    ! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/11a8f5e8-0fbf-4027-9a91-59ea12ba0eee.png)
  2. Function arguments:
    ! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/bfa64a72-fc90-4519-9904-3c17ee10988f.png)
  3. With a function that returns value:
    ! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/cf780306-41b7-4403-8190-1d08229696ae.png)
  4. Anonymous function:
    ! [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/afa4d79c-89b8-467b-a2cd-ff51c8b2fe2d.png)
  5. Since the function performed:
    ! [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/495f1bba-75ed-4192-a499-9f166160aa54.png)
  6. ES6 are allowed "arrow" (=>) defined function:
    ! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/fdc1c5d5-449b-4ba5-a275-26364a3e1b55 .png)

JavaScript function pit:

  • The return value of the function can be written more, but only returns the last, if you want to return multiple values, use an array or object.
  • Function parameters can be passed more or less than the total number of pre-defined parameters.

   
   
  1. // 1.此时传入的参数少于已定义的参数:
  2. function func(a, b) {
  3. console.log(a, b);
  4. }
  5. func( 1); // 返回值:1 undefined
  6. //(因为未定义第二个参数,所以其打印的值为undefined)
  7. // 2.此时传入的参数多于已定义参数:
  8. function func2(a, b) {
  9. console.log(a + b);
  10. }
  11. func1( 1, 2, 3, 4); // 10
  12. // 3.此时未定义参数:
  13. function func3() {
  14. var ret = 0;
  15. for ( var i= 0;i< arguments.length;i++) { // 使用for循环求和
  16. ret += arguments[i];
  17. }
  18. return ret;
  19. }
  20. func3( 1, 2, 3, 4); // 10

2. arguments parameters

! [Insert Picture description here] (http://blog.gqylpy.com/media/ai/2019-03/5f2092e7-658e-4026-bf79-3a275da91070.png)

Usually used to obtain the total number of arguments passed:
! [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/d463a8b4-fc4f-48de-a19e-16a0126b3764.png)

3. Global variables and local variables

Local variables:
variable declared inside a JavaScript function (using var) is a local variable, so it can only be accessed inside the function (the scope of the variable is the internal function), as long as the function is finished running, the local variable will be deleted.

Global variables:
Variables declared outside a function are global variables, all scripts and functions on the page can access it.

Variable life cycle:
Life on JavaScript variables from the time they are declared.

Local variables will be deleted after the function has finished running.
Global variables are deleted after the close of the page.

4. parsing

In the JavaScript function is called, it will carry out a lexical analysis.

Mr. lexical analysis will be to activate a target: Active Object (AO), and will analyze the following three aspects:

  1. Function parameter, if this parameter assigned to AO, and is undefined. If not then do nothing.
  2. Function local variable with the same name if there is no operation on AO, if not then this variable is assigned to the AO, and is undefined.
  3. Function declarations, if the AO will be covered by objects on the AO, if not then do not do anything.

Whether using the internal function parameters or local variables, are looking to the AO.

Example 1:
! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/72a1f616-93e3-4d22-94dc-81750e0b99e9.png)

Example 2:
! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/bccc6d9b-a999-41db-85cd-5b5ef18be59d.png)

Lexical analysis process:

  1. 1. The analysis parameter, and a parameter forming a AO.name = undefine;
  2. Analysis of variable declarations, there is a var name, we have found a AO.name AO above, and therefore without any treatment.
  3. Analysis function declaration, a function name () {...} statement, put into the original cover age AO.name = function () {...};

:( execution of all values during execution are looking up objects from the AO )

  1. Performing a first console.log (name), AO.name this case is a function, it is a function of the first output.
  2. Phrase var name = 'kyz'; AO.name is an attribute assignment, in which case AO.name = 'kyz', so that the second output is 'kyz'
  3. Similarly, the output of the third or 'kyz'.


 


# Built-in objects and methods

Everything is an object in JavaScript: strings, numbers, arrays, date, etc.
In JavaScript, the object is to have the properties and methods of data.

Types of Built-in objects Introduction
type of data Number Digital Object
String String object
Boolean Boolean value object
Grouping objects Array An array of objects
Math Mathematical objects
Date Date Object
Advanced Object Object Custom object
Error Error object
Function Function object
RegExp Regular expression object
Global Global Objects

Note var s1 = 'abc'; and ; var s2 = new String ( ' abc') distinguished:
[inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/! 47961b1b-b162-460b-8f5d-9991672e60d5.png)

1. Custom Objects

JavaScript objects (Object) is a bond in nature - the set of values ​​(Hash structure), the key must be a string (hashable).

Values of two methods:
! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/6ebbdc68-12de-427f-b2c2-241714adde58.png)

Traverse the content object:
! [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/d60f5b7c-474f-43e4-bbbf-395a6562204d.png)

Depth understanding

Creating objects:
! [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/72d2821c-4b54-498c-b2a6-2175f189e0ff.png)

Note:
for ES6 Map data structure is provided, which is similar to the object, but also the key - the value of the set, but the scope is not limited to the key string, values of various types (including objects) can be used as the key.
In other words, Object structure provides a string - corresponding values, Map structure provides value - corresponding value, Hash is a better structure to achieve.

Map reflects:
! [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/daa8056c-e5c2-429e-9972-531705f1e220.png)

2. Class of inheritance

  1. Basic operations like:
    ! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/1855d7a3-ae44-4481-8ad4-de33ec0e2e03.png)

  2. Inheritance (Continued Figure): !
    [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/a4e75e0c-46f8-41d8-bc86-5957ebf66fca.png)

3. Date

  1. Not specified, the default is the current time:
    ! [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/53adc635-a20e-4497-b86f-4abd4f539fab.png)
  2. String specified time, date separator may be a "-" or "/":
    ! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/e765cd49-8374- 4230-9fcc-2ea1f83ef1bc.png)
  3. Specifies the number of milliseconds:
    ! [Inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/e0a48b44-d99f-4338-9c1b-07d7f26182e9.png)
  4. Parameters for the year, month, day, hour, minute, second, millisecond:
    ! [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/698e0107-a40c-4c16-9d27 -3a8e0cbc16ba.png)

 

Date Object
method getDate() getDay() getMonth() getFullYear() getYear () getHours getMinutes() getSeconds() getMilliseconds getTime()
description day week Month (0-11) Full year year Time Minute second millisecond Cumulative number of milliseconds (initial start time)

 

4. JSON

  • JSON.stringify (obj): Serialization
  • JSON.parse (obj): deserialization

! [Insert Picture description here] (http://blog.gqylpy.com/media/ai/2019-03/ca07f635-8202-4281-bae9-46434cb6d14d.png)

5. RegExp

The basic matching syntax:
! [Here insert a picture description] (http://blog.gqylpy.com/media/ai/2019-03/fec8c730-95cd-46aa-8a88-218ce2f3a175.png)
[Here insert a picture description! ] (http://blog.gqylpy.com/media/ai/2019-03/85ae3921-c344-4598-9c44-e0cebf52c4e6.png)

JavaScript regular pit:

  1. Matching rules in unless absolutely necessary, do not leave spaces.
  2. If re with global flags G , then re.test () function does not necessarily start looking for from the beginning, but from re.lastIndex start looking for the index specified location:
    ! [Here insert a picture description] (http: // blog.gqylpy.com/media/ai/2019-03/ff078cdf-ceec-402d-b1f9-ce779a4c19cd.png) Therefore, if a second (or more) want to start from scratch to find, you need to manually lastIndex of values are reset to 0.
  3. re.test () when not specified, the default match undefined : !
    [inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/99c7e933-c98a-422e-be97-9931b59911f4 .png)

6. Math

  • abs (n): Returns the absolute value of the
  • exp (n): Returns the index e
  • floor (n): for the rounding performed when the number of
  • log (n): Returns the natural logarithm (base e) of the number of
  • max (x, y): Returns the maximum values of x and y
  • min (x, y): Returns the lowest values of x and y
  • pow (x, y): Returns the power of y x
  • random (): returns a random decimal between 0 and 1
  • round (n): rounding (Math JavaScript is no single inlet and double back)
  • sin (n): Returns the sine of the
  • sqrt (n): Returns the square root of the number of
  • tan (n): Returns the tangent of an angle

random generating random numbers:
! [inserted here described image] (http://blog.gqylpy.com/media/ai/2019-03/5fdfa360-2ca5-45bb-aed8-ec45abcf3458.png)

 

 



 

"

Original: http://blog.gqylpy.com/gqy/238

Guess you like

Origin www.cnblogs.com/bbb001/p/11366998.html