First, JavaScript introduction
JavaScript is a literal-translation scripting language, which is a dynamic type, a weak type, a prototype-based language, and a built-in support type. Its interpreter, known as the JavaScript engine, is widely used as a scripting language for the client, and is used in HTML (an application under the standard Universal Markup Language) to add dynamic functionality to an HTML Web page.
II. components
ECMAScript, which describes the syntax and basic objects of the language
The Document Object Model (DOM), which describes the methods and interfaces for working with Web page content.
A Browser object model (BOM) that describes the methods and interfaces that interact with the browser.
Three. The existence form of JavaScript code 1. There are two forms of existence:
<!--way one--><script type "Text/javascript" src= "JS file" ></script> <!--way two--><script type " Text/javascript "> JS code content </script>
2. Thinking?? Where the code exists:
There are two optional options for the above questions:
(1) The head part exists in the HTML file
(2) exists at the bottom of the body code block
Because the HTML code is executed from top to bottom, if the JS code in the head is time consuming, it will cause the user to be unable to see the page for a long time, if it is placed at the bottom of the body code block, even if the JS code is time consuming, it will not affect the user to see the page effect.
Four. Variables
Local variables in JavaScript must start with a Var, and if Var is not used, the default representation is to declare a global variable.
<script type= "Text/javascript" > // global variable name = ' Wencheng '; function func () { // local variable var age = +; // Global Variables gender = "Male" }</script>
Code comments in javascript:
- Single-line//
- Multi-Line/* * *
Five. Data type
The data types in JavaScript are divided into primitive types and object types:
- Original type
- Digital
- String
- Boolean value
- Object type
Where numbers, Booleans, nulls, undefined, strings are immutable
// null, undefined null is a keyword in the JavaScript language that represents a special value that is commonly used to describe "null values". Undefined is a special value that indicates that the variable is undefined.
1). Number Type
var a = 1;var b = 1.1;
In JavaScript, integer values and floating-point values are not distinguished, and all numbers in JavaScript are represented by floating-point numbers.
Transformation:
- parseint (..) Converts a value to a number, or Nan if unsuccessful
- Parsefloat (..) Converts a value to a floating-point number, or Nan if unsuccessful
Special values:
- NaN, not a number. Can be judged using IsNaN (num).
- Infinity, infinitely large. Can be judged using isfinite (num).
More numerical calculations:
constant MATH.E constant E, base of natural logarithm. The natural logarithm of the math.ln1010. The natural logarithm of the math.ln22. math.log10e the logarithm of E with a base of 10. MATH.LOG2E the logarithm of E with a base of 2. MATH.PI constant figs/u03c0.gif. the square of the math.sqrt1_22 is eradicated with 1. The square root of the math.sqrt22. The static function Math.Abs () calculates the absolute value. Math.acos () calculates the inverse cosine value. Math.asin () calculates the inverse chord value. Math.atan () calculates the inverse tangent value. Math.atan2 () calculates the angle from the x-axis to a point. Math.ceil () rounds a number. Math.Cos () calculates the cosine value. Math.exp () calculates the exponent of E. Math.floor () to a number of people. Math.log () calculates the natural logarithm. Math.max () returns the larger of the two numbers. Math.min () returns the smaller of the two numbers. Math.pow () calculates XY. Math.random () calculates a random number. Math.Round () is rounded to the nearest integer. Math.sin () calculates the sine value. MATH.SQRT () calculates the square root. Math.tan () calculates the tangent value.
Calculation
2) String type
var name = ' Zhaowencheng '; var name = String ("Zhaowencheng"); var name_age = String (16);
A string is an array of characters, but in JavaScript the string is immutable: You can access text anywhere in the string, but JavaScript does not provide a way to modify the contents of a known string.
Common features:
obj.length length Obj.trim () remove blank obj.trimleft () obj.trimright) Obj.chara T (n) returns the nth character in a string Obj.concat (value, ...) Stitching Obj.indexof (Substring,start) sub-sequence position Obj.lastindexof (Substring,start) sub-sequence position obj.substring (from, to) Get sub-sequence Obj.slice (start, end) slice obj.tolowercase () uppercase Obj.touppercase () based on index Lowercase obj.split (delimiter, limit) split Obj.search (regexp) matches from the beginning, returning the first position where the match succeeded (G invalid) Obj.match (regexp) Global search, if there is a G in the regular means find all, otherwise only find the first one. Obj.replace (regexp, replacement) replaced, there is g in the regular replaces all, otherwise only the first match, $ number: matches the nth group content; $&: The contents of the current match; $ ': text located to the left of the matched substring; $‘: Text $$ to the right of the matched substring: Direct amount $ symbol
3) Boolean type (Boolean)
Boolean (logic) can have only two values:truefalse. var x=truevar y=false
The Boolean type contains only true and false, unlike Python, whose first letter is lowercase.
- = = Compare Values equal
- ! = does not equal
- = = = Comparison value and type are equal
- !=== Not equal to
- || Or
- && and
4) array
Arrays in JavaScript are similar to lists in Python
Common features:
Obj.length The size of the array Obj.push (ele) tail Appends the element Obj.pop () tail Gets an element Obj.unshift (ele) The head insert Element Obj.shift () Head remove Element obj.splice (start, DeleteCount, Value, ...) Inserts, deletes, or replaces elements of an array obj.splice (n,0, val) specifies the position of the insertion element Obj.splice (n,1, Val) to specify the position of the replacement element Obj.splice (n,1) Specify location Delete element Obj.slice () slice obj.reverse () invert obj.join (Sep) Concatenate array elements to construct a string Obj.concat (Val,..) Concatenate array obj.sort () to sort the elements of a group
Six. Other actions in JavaScript
1), serialization
- Json.stringify (obj) serialization
- Json.parse (str) deserialization
2), escaping
- decodeURI () characters that are not escaped from the URL
- decodeURIComponent () non-escaped characters in the URI component
- encodeURI () escape character in Uri
- encodeURIComponent () escapes the characters in the URI component
- Escape () escapes the string
- Unescape () to escape string decoding
- Urierror is thrown by URL encoding and decoding methods
3), Eval
Eval in JavaScript is a collection of eval and exec in Python that compiles code and can get return values.
- Eval ()
- Evalerror executing JavaScript code in a string
4), Regular expression
Regular expressions are supported in JavaScript, which provides two main features:
- Test (string) to detect if a regular match
- EXEC (string) to get the contents of a regular match
Note: When you define a regular expression, "G", "I", "M" represent global matches, ignoring case and multiline matching.
5), Time processing
Time-related operations are available in JavaScript, and time operations are divided into two types of time:
- Time Unification Time
- local time (East 8 district)
Http://www.shouce.ren/api/javascript/main.html
Seven. Operators
operator = is used to assign a value.
operator + is used to add values.
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
Eight. Statements and exceptions
1. Conditional statements
JavaScript supports two medium conditional statements, namely: if and switch
If statement:
if (condition) { }Elseif(condition) { }else{ }
Switch statement
Switch (name) { case ' 1 ': = 123; Break ; Case ' 2 ': = 456; Break ; default : = 777; } Switch Statement
2. Circular statements
JavaScript supports three loop statements, namely:
1).
var names = ["Alex", "Tony", "Rain"for (var i=0;i<names.length;i++) { Console.log (i); Console.log (Names[i]);}
2).
var names = ["Alex", "Tony", "Rain"]; for (var in names) { console.log (index); Console.log (Names[index]);}
3).
while (condition) { // break ; // continue;}
3. Exception Handling
Try { // This piece of code runs from top to bottom, where any one of the statements throws an exception the code block ends running }catch (e) { // If an exception is thrown in a try code block, the code in the catch code block is executed. //e is a local variable that points to the error object or other thrown object }finally { // the finally code block is always executed, regardless of whether the code in the try is thrown abnormally (even if there is a return statement in the try code block). }
Actively throws exception throw error (' xxxx ')
Nine. Functions
1. Basic functions
The functions in JavaScript can basically be divided into three categories:
// Common Functions function func (Arg) { returntrue; } // Anonymous Functions var function (ARG) { return "Tony"; } // self-executing functions (function(ARG) { console.log (ARG); }) (' 123 ')
2. Scope
Each function in JavaScript has its own scope, and when a function is nested, a scope chain appears. When the inner-layer function uses a variable, it is based on the scope chain from the inner to the outer layer of the loop, and if it does not exist, the exception.
Remember that all scopes exist when the function is created and not executed.
function F2 () { var arg= 111; function F3 () { console.log (ARG); } return = F2 (); ret ();
There are a few things about the scope:
No block-level scope in 1.JavaScript
2.JavaScript using function scope
Scope Chain of 3.JavaScript
4.JavaScript The scope chain was created before execution
5. Declaration in advance: JavaScript functions are declared before they are executed, without assigning a value.
3. Closed Package
A "closure" is an expression (usually a function) that has multiple variables and the environment in which those variables are bound, and therefore these variables are also part of the expression.
A closure is a function, and it "remembers what's happening around". Represented as "another function" defined by "one function" body
Because the scope chain can only be found inward, the function internal variable cannot be obtained by default. A closure that gets the variables inside the function externally.
function F2 () { var arg= [11,22]; function F3 () { return arg; } return = F2 (); ret ();
4. Object-oriented
function Foo (name,age) { this. name = name; this. Age = Age ; This function (ARG) { returnthis. Name + arg;} } var New Foo (' Zhaowencheng ', +); var ret = obj. Func ("learnning"); Console.log (ret);
- Foo acts as a constructor
- This refers to the object
- When creating an object, you need to use the new
Each object in the preceding code holds an identical func function, wasting memory. The problem is resolved as follows:
function Foo (name,age) { this. name = name; this. Age == { function() { return"This" . Age }, function(ARG) { returnthis. Name + arg;} }
Wencheng Small Pots Python-num15-javascript Foundation