20150203+js consolidation and strengthening 1-01

Source: Internet
Author: User
Tags arithmetic operators logical operators

JavaScript consolidates and strengthens a

First day: JavaScript review + function + scope chain +script code execution + array

Two chains: Scope chain + prototype chain

One pack: Closures

Day Two: Event programming

Third day and fourth day: Object-oriented + Snake game development

Day Fifth: Regular Expressions One, Introduction 1, Why do I need JavaScript?

2. What is JavaScript

JavaScript is a Web page scripting language developed by Netscape (Netscape) based on client-side browser, object-oriented, event-driven.

client browser:

HTML, CSS, JavaScript client language

php/java/. NET Server language

Determine whether a language is a client or a server side, mainly to see where "it" is running

JavaScript is an object-oriented scripting language

Event-driven 3, JavaScript application scenarios

1) Web Effects

2) Form Validation

3) Web games (Super Marie, Gobang, Tank Wars)

4) server-side programming (Ajax, node. js) 4, QuickStart

Example 1: Output Hello World via JavaScript

Script tag: JavaScript identifier

Language: Tell the browser which language to parse when parsing

In today's browsing, you do not need to specify the language property in script, because the browser default recognizes that the script tag is automatically parsed with the JavaScript engine

Example 2: Introducing an external JS file via the SRC attribute of the script tag

Suggestion: In JS, how to determine the location of the JS code placement

1) for function declaration, class definition, external file import, it is recommended to put in head tag

2) corresponding function call, code execution suggestion put in body tag ii. JavaScript Basic Syntax 1, JavaScript basic format

1) JavaScript case-sensitive

var i is different from Var i

2) JavaScript scripts must be embedded in HTML files

3) JavaScript script cannot contain HTML tag code

If you want to use HTML code in JavaScript, you can take document.write ("

4) Write a script statement per line

document.write (' hello '); alert (' World ');

5) Add a semicolon at the end of the statement (it is recommended to add semicolons)

document.write (' Hello World ')

Alert (' Hello JavaScript ');

6) JavaScript script can be saved independently as an external file

Public.js file, and the SRC attribute in script directly introduces 2, variable

Variables are containers used to store values temporarily, and the values stored in variables can vary.

Variables must be declared before they can be used, variable declared with Var

The wrong:

alert (i);

Correct:

var i=10;

alert (i);

Correct: (global only, not recommended)

i=10;

Alert (i)

Naming rules for variables: the first character must be an English letter, or an underscore (_), or a $ dollar sign, followed by a character that can be an English letter, a number, an underscore, or a variable name that cannot be a reserved word for JavaScript

Scope of the variable: global variable, local variable 3, data type of the variable

JavaScript Weak type

1) String character type

In PHP, single and double quotes are different, but in JavaScript the single and double quotation marks are the same

var str = "Hello world";

var str = ' Hello world ';

2) Number integer, float, double-precision floating-point

var i=10;

var i=10.00

3) Boolean true, False

4) Undefined (variable declaration, but not assigned value)

5) Null empty type

6) object Type (array)

4, operator 1) arithmetic operators

+ - * / %

+ +: Self-added

--: self-reduction

var i=10;

alert (i++); First performed in Gaga

var i=10;

alert (++i); First Gaga in execution 2) comparison operator

> < = = = = = >= <=! =!==

= =: Value equal, type can be different

= = = All equals, required values are equal and type is consistent

3) Logical operators

&& | | ! 4) Assignment operator

= + = = *=/=%= 5) string operator

+ 5, Process process 1) sequential structure

Example 1: Getting the contents of a DOM object and changing its value

2) Branching structure

If else if else

Switch

Example 2: Judging the current day of the week

Description

In the IF structure, the judgement of the fixed value can be realized as well as the range.

In switch, only the fixed value can be determined 3) loop structure

For loop

While loop

Do...while ... Cycle

For...in ... Cycle

Example 1:

When to use a for loop, when to use a while loop?

We usually take a for loop structure for the number of known loops

For the number of unknown loops, we usually use the while loop structure

Do...while ... The difference from the while

Do...while ... : First execution after judgment

While: first judgment and then execute

From the angle of cycle times:

Do...while ... Always perform an exercise more than while:

1. Write 99 multiplication table

2, there is a thing, do not know its number, 33 number of two, 55 number of three, 77 number of two, asked the geometry of the matter? (within 100) Iii. functions in JavaScript 1, function classification

1) System functions

2) Custom function 2, custom function

The function must be defined before it can be called.

The L function definition has three parts: function name, argument list, function body

L define the format of the function

function name ([parameter 1, Parameter 2 ...]) {

function execution part;

return expression;

} 3, functions of the function:

1) Code Reuse

2) Modular programming 4, Quick Start

Example 1: Implementation of the greeting program in JS

Example 2: Because the above code has a lot of repetitive code, you can use the function to encapsulate

Example 3: Improve the title, achieve different greetings to different people

5. Function parameters

Arguments: Parameters that are passed when a function is called, which we call an argument

Formal parameters: When a function is defined, the specified parameter, which we call the formal parameter

Example 4: Changes to function parameter values do not affect arguments (except objects)

Example 5: improved Example 4

From the view of software design, the case of example 4 is found that it does not meet the "high cohesion, low coupling" software design idea

Cohesion Poly-Low coupling: Try not to rely on external factors to operate and function-minded

6. function in memory representation

Description

function in memory is a contiguous memory address, placed in the code snippet, when we define a function in the program, the system will automatically return its first address in memory, and call or execute the function, the system will automatically find the address and execute

Note: When defining a function, the system automatically returns the first address of the function

Example 6: In fact, our function is also a special quantity

By testing we found that variables can store any amount of data, so since the function is also a quantity, then I mean that our variables can also store the first address of the function

by executing I (), a call to a function can be implemented, with the following code:

Example 7: Because of the above case, in the assignment process, the function name does not make any sense, so you can remove

We call a function without a name as an anonymous function.

Note: Even if the anonymous function is defined, the system will automatically return the first address of its function

7. Self-invocation of anonymous functions

Self-invoking anonymous functions:

In the future project development, often introduce a variety of JS code library, such as jquery, ExtJS, Dojo, may be used in the process, there will be a function conflict, in order to solve the function conflict problem, we usually take the self-invoke anonymous function to solve the problem.

20150203+js consolidation and strengthening 1-01

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.