JavaScript (three) - function, variable scope, methods

1. Function

In JavaScript, the function that is the object , can be freely controlled procedures, functions defined can be nested in other functions, so that they can access any variable is a time defined scope.
( 1) a defined manner (in absolute value function abs () as an example)
Here Insert Picture Description
(2) Second way defined
Here Insert Picture Description
function (x) {...} This is an anonymous function, but the results can be assigned to abs, you can call the function by abs , a manner mode is equivalent to two

(3) call
when calling the function, value that can be passed, these values are referred to as parameter. javaScript can pass any parameters, or may not pass parameters .
Here Insert Picture Description
Determining whether the parameters passed in problems , if the problem is manually thrown.
Here Insert Picture Description
Here Insert Picture Description
(4) arguments The
arguments The JS is a free gift of keywords; all of the parameters passed in representatives, is an array !
Here Insert Picture Description
Here Insert Picture Description
Question: arguments contains all the parameters, we sometimes want to use the extra parameters to append operation. Need to exclude existing parameters -

(5) rest
new features introduced ES6, obtaining all the parameters in addition to parameters already defined .
rest parameters can only write in the final surface , and must be used to identify ...
Here Insert Picture Description
Here Insert Picture Description

2. The scope of variables

  • In javascript, var is actually define the variable scope, a scope of access to the variable collection object, functions.
  • In the variable outside the function declaration , called a global variable , because it can be accessed by any other code in the current document. In the variable declared inside a function , called a local variable , because it can only be accessed within the current function.

(1) a statement in the function body, in vitro function is not used (in order to achieve a non-words, can look back closures)
Here Insert Picture Description
(2) If there are two functions use the same variable name, as long as the internal function to do not conflict
Here Insert Picture Description
(3) functions can access the internal members of the external function, but not vice versa
Here Insert Picture Description
Here Insert Picture Description
(4) variable external functions and variables inside a function of the same name, the "inside" to "outside" Find
Here Insert Picture Description
Here Insert Picture Description
Description : Find function in JavaScript variables from their own function begins, 'to' outside 'the look' inside ' . Suppose the function variable with the same name exists outside the inner shield function of variable external function.

(5) enhance the scope of variables

  • First use a variable and then later declare variables and not throw an exception, as the lifted variable
    Here Insert Picture Description
    Here Insert Picture Description
    Description : JavaScript variables feel is "promoted" or moved to the front of the function or statement. However, after the lifting variable will return undefined value that will not enhance the assignment variable y.

  • Regulate the operation
    Here Insert Picture Description
    of all of the variables are defined on the head of the function , not misplacing easy code maintenance.

(6) global function

  • Global variables
    Here Insert Picture Description
    Here Insert Picture Description
    (7) global object window
    Here Insert Picture Description
    Alert () function itself is a window variable
    Here Insert Picture Description
    Javascript is actually only a global scope, any variable (function can also be considered variable), assuming that is not found in the function scope, will be to outside look, if not found in global scope, error RefrenceError

  • Specifications
    Because we all global variables are bound to our window. If a different js files, use the same global variables, conflicts occur .
    Here Insert Picture Description
    Solution : put all your code into their own unique name space definition, reducing the global problem of naming conflicts. jQuery

(8) the local scope: let
Here Insert Picture Description
Here Insert Picture Description
can be seen from the definition of local variables var , the console 11 is outputted from this number, the implementation of the console.log (i + 1) statement, I out of the scope may also be used . This is not standard!

ES6 introduction of the let keyword , local scope resolve conflicts
Here Insert Picture Description
Here Insert Picture Description
suggest that you are let go by the definition of variables local scope

(9) Constant const

In the prior ES6 , how to define constants: Only use all capital letters named variable is constant; recommended not to modify this value
Here Insert Picture Description
in ES6 introduced constants const keyword
Here Insert Picture Description

3. Methods

(1) define the method
is to put on the function of the object inside , objects are only two things: the properties and methods
Here Insert Picture Description
Here Insert Picture Description
this is not the point, is the default pointing to it that object calls

(2) apply in this point can be controlled js
Here Insert Picture Description

4. Internal Object

Standard objects
Here Insert Picture Description

4.1 Date

Date date and time for processing objects
Here Insert Picture Description
console output
Here Insert Picture Description
conversion
Here Insert Picture Description
console output
Here Insert Picture Description

4.2 JSON

Early on, all the data transmission accustomed to using XML file

  • JSON (JavaScript Object Notation, JS objects notation) is a lightweight data-interchange format .
  • Simple and clear hierarchy make JSON an ideal data-interchange language.
  • Easy to read and write, but also easy for machines to parse and generate, and effectively improve the efficiency of network transmission.
  • In JavaScript everything is an object, any type of support js can use JSON to represent; number, string ...

Format :

  • Object with {}
  • Arrays are used []
  • All key-value pairs are used key: value

Conversion JSON string object and JS

  • The JSON.stringify () ; the JS objects into JSON string
  • The JSON.parse () ; JSON string into JS objects
    Here Insert Picture Description
    Here Insert Picture Description
    distinguish objects JSON and JS
    Here Insert Picture Description

var obj = {a: 'hello ', b: 'hellob'}; JS objects
var json = { "a": "hello", "b": "hellob"}; JSON string, the key must be added ""

4.3 Ajax

  • AJAX = Asynchronous JavaScript and XML (Asynchronous JavaScript and XML) .
  • AJAX is not a new programming language, but using a new method of existing standards .
  • The biggest advantage is AJAX without reloading the entire page, you can exchange data with the server and update parts of the page content.
  • AJAX does not require any browser plug-ins, but requires the user to allow the execution of JavaScript in the browser
  • Js native asynchronous request written xhr
  • Method jQuey packaged $ ( "# name"). ajax ( "")
  • axios request
Published 62 original articles · won praise 2 · Views 2741

Guess you like

Origin blog.csdn.net/nzzynl95_/article/details/103915706