20150203+js consolidation and strengthening 1-02

Source: Internet
Author: User
Tags array definition

Iv. scope chains in JavaScript

Example 1: global scope and local scope

Description: What we call global variables and local variables is actually relative.

Example 2: Local variables can not be accessed directly in the global scope

Answer: The above situation is not allowed, reason:

1) because the scope chain does not allow

2) Because of the JavaScript garbage collection mechanism

Example 3: Discussing the relationship between function and function

By observing, the above code can be found to execute normally

Example 2: If the function is in a function, then there is also a scope relationship, as shown in

By running the above code, the system will prompt fn1 () missing objects, mainly because when the 15th row is executed, the system will automatically reclaim the memory space occupied by the display function, causing the call to fn1 function cannot be found, resulting in a missing object error.

Review the PHP code, define the function inside the function, then can it be accessed outside the function?

Example 3:

By running the above code, the result is returned DISPLAYFN1

Which means we can call function 1 in PHP, and delve into the scope of JavaScript and PHP.

Scope Issues in PHP:

In the process of running PHP code, the system will first load the display function and open a contiguous memory space in the running stack area, when the display function is called, the system will automatically open a separate and parallel memory space for FN1 storage in the running stack, because the two are in parallel independent relationship , so I can call the FN1 function directly after the display call is complete.

JS Scope

While the JavaScript code is running, the display function is first loaded and a contiguous amount of memory is opened in the running stack, and when the display function executes, The system will automatically continue to open up a contiguous memory space within the scope of the display function for saving the FN1 function, because we have no way to directly call the FN1 function outside of the function because the two are in the containment relationship.

Example 4:

By observing the above code, we will find that the system pops up 100, Reason: 2, scope chain

When we call a variable inside the function, the system first looks for the Var declaration statement (Var i) in the current function, and if it is found, it will be used directly, otherwise it will continue to look for it, if it is found, use it directly, otherwise continue to look up until the global scope, if found, is used, Otherwise the variable is declared in the global scope, and we call this process the scope chain

Scope chain and schematic diagram:

Example 5:

The above code pop-up result is 1000, the principle is ibid.

Example 6:

Running results: 1000,1000 v. Script code Snippet 1, JavaScript script snippet execution process:

1. Read into the first code snippet

2. Compiling

Declaring variables, declaring functions, grammar checking, semantic checking, code optimization, parsing and getting the code tree

3. Implementation

Code calls, code execution

var i = 100;

Compile process: var i = undefined;

Execution process: i=100;

4. Read into the next code snippet

5. Compiling

6. Implementation

7 、......

8. End 2, explore the impact of code snippet errors (compilation errors, execution errors)

Example 1: Impact of compilation errors on the current code snippet

The code cannot be executed by running discovery. Reason:

We know that code execution is divided into two phases:

The compile and execute phase, we do not write parentheses, then the system will generate errors during the compilation process, so the subsequent code and the preceding code will not be executed properly.

Description: A compilation error could result in the current code snippet not being executed

Example 2: Impact of execution errors on the current code snippet

Operation Result: Hello, error

Description: The current code in the compilation phase without any exception, at run time, first run the 9th line of code, run to the 10th row, the system cannot find the display function, so error (execution error), will cause the subsequent Word cannot output, so it can be explained:

Script snippet execution error only affects code behind the current code, without affecting the execution of the preceding code

Example 3: Impact of compilation errors on subsequent code snippets

Example 4: Impact of execution errors on subsequent code snippets

With Example 3 and Example 4, we can conclude that both compilation errors and execution errors affect only the current code snippet, without affecting the subsequent code. 3, script code snippet execution schematic diagram:

Interview question: Judging the execution result of the current program?

Execution Result:


Description: When the function executes to 第9-10, the system defines the global variable i=100 with the declaring function, when it executes to line 15th, the system calls the display function, because the 11th line of code executes alert (i), because of the scope chain principle, the system first looks for the Var declaration in the current scope, The Var declaration was found in line 12th, and the code execution was compiled and executed in two phases: since the function display inherently compiles with Var i=undefined, when the 11th line of code is executed, the system first outputs undefined, and when it executes to 12 rows, The system automatically assigns a value to the variable i=1000, when executed to 13 rows, the system automatically pops up the variable i=1000 that has just been assigned, so two times the result is undefined and 1000. Vi. arrays in JavaScript 1, what is an array?

A: A collection of sets of data

One-dimensional arrays

Two-dimensional arrays

Multidimensional array 2, array definition

L var arr=[value 1, value 2, value 3]; Implicitly-Created

l var arr=new Array (value 1, value 2, value 3); Display creation or direct instantiation

l var array=new array (size); Show create and specify array length

Example 1: Creating an array in the above three ways

Example 2: How to reference a defined array

Basic syntax:

Array name [index]

Example 3: How to traverse the output array data

1) traversal output via for loop

The array has an attribute length, which identifies the array lengths

Operation Result:

2) through for...in ... to traverse the output

Output Result:

Example 4: two-dimensional array definition

Operation Result:

Example 5: Two-dimensional array traversal

Operation Result:

3. Define text subscript Array

Example 6: Text subscript array

Operation Result:

Pop Up Mei

Example 7: The text subscript array element is not counted in the array length

Output: There are 3 elements in the current array

IE debugging tool, F12 popup, such as

The debug results are as follows:

In the array, our text subscripts are appended to the array as attributes, so it does not count toward the array length.

Example 8: Traversing output with text subscript array

Example code:

Operation Result:

VII: Job assignment one: System functions related to array arrays in JavaScript

To add an array element:

L Arrayobject.push (newelement1,newelement2,...., newelementx)

L Arrayobject.unshift (newelement1,newelement2,...., newelementx)

L Arrayobject.splice (index,howmany,element1,....., elementx)

To delete an array element:

L Arrayobject.pop ()

L Arrayobject.shift ()

L Arrayobject.splice (Index,howmany)

Interception and merging of arrays:

L Arrayobject.slice (Start[,end])

L Arrayobject.concat (Arrayx,arrayx,......, Arrayx)

L Returns a new array

Array of connections:

L Arrayobject.join (separator)

Array of cuts:

L Stringobject.split (separator, howmany) job two: Provincial and municipal cascade (two-level linkage)

Beijing (Haidian District, Chaoyang District)

Guangzhou (Tianhe, Haizhu District)

20150203+js consolidate and strengthen 1-02

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.