JavaScript basics from shallow into deep understanding (i)

Source: Internet
Author: User
Tags script tag

Introduction to JavaScript

JavaScript is a dynamic, weakly-typed, interpreted programming language that enhances page dynamics and enables real-time dynamic interaction between pages and users.

JavaScript is made up of three parts: ECMAScript, DOM, BOM

ECMAScript is defined by ECMA-262 and provides core language functionality (ECMA is the European Association of Computer Manufacturers)

DOM Document Object Model, which provides methods and interfaces for accessing and manipulating Web page content

BOM (Browser object model) browser objects models that provide methods and interfaces for interacting with the browser

1. Three ways to use JS and comments
// (1) directly in the HTML tag, directly use the time attribute, call JS code <button onclick= "alert (' Point Me ')" > Point me! </button>//(2) Insert the JS code using the script tag anywhere on the page. <script type= "Text/javascript" >    alert (123)</script>//(3) Introduce an external JS file. You can save the script to an external file. External files typically contain code that is used by multiple Web pages. The file name extension for external JavaScript files is. js. <script src= "01.js" type= "Text/javascript" ></script>

Attention:

1.JS code can be placed anywhere on the page, but the placement of different positions will affect the order of JS execution.

2. The script tag that introduces the external JS file can no longer contain any JS code.

JS's comment
// single-line comment   ctrl+//*  Multiline comment ctrl+shift+/     * /
JS in the Basics 1. The variables in JS:

(1) Declaration of variables: use the var keyword declaration. var num = 1;

(2) Variables may not be declared or can be directly assigned: num = "haha haha";

(3) Declare multiple statements with one line of code: VAR a=1,b=1,c. where B is undefinde.

The difference: Variables declared with VAR are local variables, valid only at the current scope (equivalent to local variables), variables declared without var are global variables by default, and can be used throughout the JS file.

Considerations for variable declarations in 2.JS

(1) The keyword in JS that declares a variable is only Var, and the type of the variable depends on the type of assignment. If the declaration is not assigned a value, the undefined type.

(2) JS in the same variable, can be in multiple assignments, the data type is modified. var a=1; A= "dddd"

(3) Variables can be declared using Var, or directly assigned to a value. (The scope of the Var declaration is a local variable)

(4) In JS, a variable can be used multiple times VAR declaration, the subsequent declaration is equivalent to direct assignment, no OVA.

(5) JS variables are case-sensitive, and uppercase and lowercase are not the same variables.

data types in 3.JS

(1) Undefinde: A variable that is declared with VAR but has no assigned value.

  (2) Null: Represents an empty reference.

(3) Boolean: TRUE or False

(4) Number: In JS, numeric types include shaping and floating-point.

(5) String: Strings

(6) Object: Objects

4. Common numeric Functions

(1) IsNaN: Used to detect whether a variable is non-numeric (not a number). When detected, the number function is called first, attempting to convert the variable to a numeric type, or Nan if the final result can be converted to a numeric value. The
(2) Number function: Converts various data types to numeric types.
>>> Undefined: Cannot convert, return Nan.
>>> null: Convert to 0
>>> boolean:true to 1,false to 0
>>> string: If the string is a pure numeric string, it can be converted; The string contains non-numeric characters, it cannot be converted, and Nan is returned. If the empty string is converted to 0.
(3) parseint (): Converts a string to a numeric type.
>>> cannot go if it is an empty string. "" "-->nan
>>> if it is a pure numeric type string, it can be converted, and the decimal point is not preserved directly. "12.3"-->12
>>> if the string contains non-numeric characters, the integer that precedes the non-numeric character is converted. "123a"-->123; "A123"-->nan
(4) Parsefloat: The conversion mechanism is the same as parseint.
The difference is that when you convert a numeric string, you can keep the decimal point if the string is a decimal, and the integer type has no decimal point. "123"-->123    "123.3"-->123.3
(5) typeOf (): detects the numeric type of a variable
   string-->string Numeric-->number  true/false-->boolean  undefined-->undefind  object/null-->object  function-- function  

input and output statements commonly used in 5.JS

(1) alert (): Pop-up window output

(2) prompt (A, b): Pop-up window input. Receive two parts: a--prompt content; b--The default text for the input box. (both parts can be omitted). The input content is a string by default.

(3) document.write (): Prints the output in the browser page.

(4) Console.log (): Browser console printing.

operators in 6.JS (similar to Java, where only different places are described)

Different from Java:

(1) Division sign: No matter whether the symbol is an integer or a decimal number, the decimal will be retained according to the actual result. (22%10=2.2)

(2) = = = Absolute Equals (the value and data type must be the same), and = = To determine whether the data on both sides is the same, do not care whether the data types on both sides are the same.

(3) & | : Bitwise operations can only be performed if the two sides are not numeric types and are converted to numeric values after the operation.

(4) && | | : Only logical operations can be performed.

The IF in JS

(1) Boolean:true is true, False is False

(2) value type: 0 is false, not 0 is true

(3) String type: empty string is false, non-empty string is true

(4) Null/undefined/nan: All False

(5) Object: Objects are all true

The Swich in JS:Swicht (various data types), compared to the use of = = = To determine the data type completely want to wait. The declaration of the function is called 1. The format of function declarations in JS
function function name (parameter 1, parameter 2,.....) {    // function Body Code    return return value;}
2. Invocation of the function:

(1) Call directly: function name (parameter 1, parameter 2,.....)

(2) Call via event: In an HTML tag, it is called through the event properties.

Precautions:

(1) There is no return value in the function, only depends on the function has no return, no need to declare. In JS, there is a return value can not receive, no return value, can also receive, the result is undefined.

(2) The formal parameter list of a function in JS is not associated with an argument list, that is, parameters can be unassigned, and no parameters can be assigned. The unassigned parameter is undefined. The actual argument list of the function depends on the argument list.

(3) The function in JS is the only scope of the variable. (except for functions, other ways have no scope, that is, global variables). The formal parameters of a function are local variables.

(4) The Declaration of the function and the calling statement have no precedence, that is, the call statement can be written before declaring the function.

3. Self-executing functions
(1)! function (){}(); // start using! Indicates that this is a self-executing function. (2) (function() {} ())// use () to declare the anonymous function with the calling package. (3) (function() {}) ()// use () to package an anonymous function declaration statement

NOTE: The call statement for the function must be placed after the declaration statement.

[Execution order of JS code]

JS code execution is divided into two phases: check the compile phase and the code execution phase.

Check the compilation phase: The main check syntax errors, the declaration of variables, functions, such as declaring operations.

Code Execution phase: the assignment of variables, the invocation of functions and other execution statements, belong to the code execution phase.

4.arguments Objects

1. Function: Used to save all the arguments of a function.

>>> when a function has an argument, you can use an array subscript to access all the arguments of the function. Alert (Arguments[6])

2, the number of elements in the arguments depends on the argument list, regardless of the number of parameters.

3, once the function is called, the arguments are passed in, then the corresponding bits of the formal parameters will be arguments corresponding elements to bind, modify the parameters, the corresponding elements in the arguments will be changed, and vice versa is also established. However, if a location does not have an incoming parameter, the parameters and arguments are not associated.

Arguments.callee (): a reference to the current function used to call itself recursively in a function.

JavaScript basics from shallow into deep understanding (i)

Related Article

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.