JavaScript
// This is the comment a = 1; // Simple Assignment Statement
Data type
JS has a dynamic type, that is, the same variable can be used for different types (Python is also)
numbers |
number |
var x = 5;//There is only one numeric type var x = 5.0; var x = 5e5; var x = 5e-5; All data is stored as 64-bit floating-point data |
string |
string |
var x = "5"; |
Boolean |
boolean |
var x = true; var x = false; |
array |
array |
var x = new Array (); x[0] = 0; var x = new Array ("0", "1"); var x = ["0", "1"] |
Object |
object |
//object is a container for properties and methods var x = {Zero:0,first:1};//name:value var Z Ero = x.zero;//Access object property var first = x["First"]; |
function |
function |
A = function () {} |
null |
Null |
var x = Null;//null can be used to empty variables |
undefined |
Undefined |
var x;//variable does not contain a value null and Undefined values are the same, but different types |
Functions, Methods
Function |
Method |
|
Defining a function in Object x funcname |
Function name (var1, var2) { var x = 1; return x;//returns x } |
var x{ Id:1, Funcname:function () { return ID;};}; |
|
X.funcname; X.funcname (); |
Return by default is the last line in the function, even if no semicolon (return) is the default ending function.
Variable Scope
|
Local variables |
Global variables |
Declaration/Definition Method |
defined in the function body var x = 1; |
Assign a value to an undeclared variable, automatically as a global variable x = 1; Variables declared outside of a function var x = 1; |
Scope |
can only be accessed within a function |
can be accessed from within the page |
Survival time |
Delete after function finishes running |
Delete after page closes |
|
|
Global variables are part of the Window object x = 1; window.x = 1; for (var i = 0; i < i++) { var y = i; } I and Y can be accessed outside the loop |
most operators are similar to C + +
Algorithm operators |
+ - * / ++ -- % |
Assignment operators |
= += -= *= /= %= |
string + number |
string + Number = string "A" +5= "A5" 5+5+ "a" = "10a" Empty text + number = "number" (no spaces) "" + 5 = "5" |
Comparison operators |
= = = = = = = Absolute equals (value and type) ! = does not equal! = = Not absolute equals (value and type)
< <= > >= |
logical operators |
&& | | ! (not) Compare equations? True Result: false result |
Unary operator + |
var y= "5"; var x=+y;//use + to convert variables to numbers |
conditional Statements are similar to C + +
If...else |
Switch |
For loop |
While loop |
if (a>b) {} Else if (a>c) {} Else {} |
Switch (n) { Case 1= 1; break; Case 2= 2; break; default = 0;} |
for (int i=0; i<len; i++ ) {} // traverse every property in an object var data={x:0,y:1}; for (d in data) {} // iterates through each element in the array var data=["A", "B" ]; for (d in data) {} |
while (true) {} Do {}while (true) |
Break and Continue are similar to C + +
Continue can only be used for loops
break; can only be used for loops or switch break tag references; can exit any block of code
// List_label Mark the following statements = 1= 1; // Break Tag Reference to exit any block of code Break = 1;}
typeof getting variable types
typeof 1; typeof NaN; |
Number When converting, it is Nan that cannot be converted to a legal digit |
typeof "1"; |
string |
typeof true; |
Boolean |
typeof []; |
Object |
typeof null; Var x; typeof X; |
Object Declares no value only |
typeof undefined typeof x; |
Undefined x not declared |
typeof fcuntion () {} typeof fcuntion A () {} |
Function |
"1". Constructor |
The constructor property can return the object's constructor, So it can be judged by the object's Consructor property Return to array typeof is also object, but you can return array () |
with the constructor property
Error (Exception)
Try { throw "error message";//Throw a custom error} Catch = The value of Err;//err is the error message}
Variable Promotion
1. Declarations of functions and variables are automatically lifted to the top (the declaration automatically becomes the top of the code)
2. Initialization does not automatically increase (var x = 5; The position of this statement does not change automatically)
Strict mode
This mode has certain requirements for the code. Use strict mode by writing "User strict" in the script or function header.
Json:javascript Object Notation
A lightweight data interchange format used to store and transmit data in a format that is typically used for service-side data transfer to a Web page.
Grammar rules
The data is a key/value pair. Data is separated by commas. Curly braces Save the object. Square brackets Save Array
3 Array of site information (objects)
{"Size=":[ {"Name":"Runoob","Url":"Www.runoob.com"},{"Name":"Google" "url" : "www.google.com" }{ " Name ":" Taobao< Span class= "hl-quotes" > "" url" : "www.taobao.com" } ] }
javascript:void (0)
< Span class= "hl-quotes" > < Span class= "hl-string" > < Span class= "hl-quotes" >javascript:void (0) void evaluates () An expression but does not return a value. <a href= "javascript:void (0)" ></a> click on link will not react. However, when used in JS, void does not produce an effect.
The difference between href= "#" and href= "javascript:void (0)"
# contains a location information, the default anchor is #top is the top of the page.
and javascript:void (0), only represents a dead link.
JS Learning--Basic knowledge