JavaScript syntax and rules
First, a comment
1, single-line comments
//
2, multi-line comments
/**/
Second, variable
1, the statement
var 变量名; //默认值为undefined
2, the assignment
var 变量名=值;
3. Requirements
When you declare a variable in Javascript, you need to follow the following naming convention:
- Must begin with a letter or an underscore, the middle can be numbers, or the underscore character
- Variable names can not contain spaces and other symbols ·
- Javascript can not use keywords as variable names, such as: function
- Javascript strictly case-sensitive.
4, basic data types
Similar to the basic data types in java
string | String type | '' And '' are strings, JavaScript is not a single character |
---|---|---|
boolean | Boolean | Fixed value is true and false |
number | Digital Type | Any number |
null | air | A placeholder |
undefined | Undefined type | undefined |
Note: Because undefined is derived from nul, the so undefined == null
Javascript is different from java, is weakly typed language, do not use variables strictly follow the norms, so good after a variable declaration, can be assigned a value of any type.
By typeof
which the basic data types of variables can be distinguished
or null object, is not a bug tube
5, reference data types
Reference data types are processedObjects
Create a way
var str = new String(); //和java相同
var str = new String; //js独有
//都可以
Third, the operator
js operators and consistent java
The following is a java js and different places
1, comparison operators
== | equal |
---|---|
=== | Congruent (type value) |
!= | not equal to |
> | more than the |
< | Less than |
>= | greater or equal to |
<= | Less than or equal |
== logic, etc., only the ratio
=== congruent, and comparison of the ratio of type
Compared with the same type and valuetureOtherwise, compared withfalse
2, logical operators
&& | and |
---|---|
|| | or |
! | not |
no js &
Fourth, a regular target
1, to create a way RegExp object
var reg = new RegExp("表达式"); //基本不用
var reg = /^表达式$/; //常用,直接量
The amount of regular direct way is the object instead of a string
Suitable for form validation
2, test method
Will check whether the regular
var reg = /^表达式$/;
var flag = reg.test("字符串");
alert(flag);
Match returns true, does not match the returns false
3, the normal way
var reg = /表达式/;
Unless all of the characters do not meet the regular, it returns false
As long as there is a character in line with regular return true
Applies to string search, replace
Five, JS array of objects
1, characteristics
JS array can be seen in JavaArrayListset
Each member of an array type is not limited, and can store any type
Length of the array can beAutomatically modified
2, four kinds of ways to create
1、var arr = [1,2,3]; //最常用
2、var arr = new Array(); //数组长度默认为0
3、var arr = new Array(4);
//数组长度是 4, 相当于开辟了长度为 4 的空间,每个元素是 undefined。
//(仅在显示数组时进行处理,undefined 为了方便显示变成了空字符串形式,其值本质仍然是 undefined)
4、var arr = new Array(1,2); //数组元素是1,2
3, commonly used method
W3school
join()不会对原数组产生影响
reverse()会对原数组产生影响
Sixth, global function (global)
1, execution
eval()
Role: for scalability enhancements program
Note: You can pass only the original data type string, pass a String object no effect.
eval("var x = 10;");
alert(x);
2, encoding and decoding
URL encoding: Chinese special symbols and hexadecimal%
Role: to ensure the integrity of the data transfer.
encodeURI()
Encoded URI string
decodeURI()
Decoding an encoded URI
3 difference, URI and URL's
URI is a uniform resource identifier. Name identifies the resource in detail.
URL is a Uniform Resource Locator. Network location positioning resources
Resources: information can be accessed through a browser called all resources. (Pictures, text, HTML, CSS, etc.)
Details Name URI identifies resources, including the resource name.
Network location URL to locate resources. Include http: //
E.g
http: /www.huangjihao.com/ URL is
/a.html Shi URI
htp: //www.huangjihao.com/a.htmlBoth a URL, URI is
4, the string-to-digital
a、parseInt()
Parse a string and returns aInteger
b、parseFloat()
Parse a string and returns aFloat
If a character string from the literal can not be converted to digital, then the conversion from the character began to stop, only to return in front of the correct conversion values.
If the first character of the string can not be converted to digital from the literal, then stop the conversion, return NalN NaN (Not A Number, a numeric identifier to indicate not a correct number)
Seven custom functions / custom methods
1, the function format
function 方法名(参数列表){
函数体
}
2 Notes
No definition Javascript functions return type, a method of writing directly behind the function name;
Define parameters without using the var keyword, otherwise an error;
Java Script function body, return can not write, you can return a specific value, or simply write return
Javascript function call is finished there must be a return value, the return value is determined according to the type and, if no specific return value, the return value is undefined;
If Javascript functions with the same name, then there is no method overloading, each covering only method,Prior to the last defined function covered by the definition
Because Javascript function overloading does not exist, so only in accordance with Javascript method name to call a function, even if the real shape to participate in the function parameters do not match, it will not affect the normal call
If the parameter is not assigned, the default value
undefined
Eight custom object
1, function constructor
format
function 对象名(){
函数体
}
//
function Person(n,a){
this.name = n; //定义阶段,定义属性
this.age = a;
}
var p = new Person("小黄", "20");
alert(p.age)
alert(p.name)
p.gender = "男"; //创建对象后定义属性
2, the amount of object directly
The way to create an instance of the object directly, without building function, no longer need to create a new instance of the object, can be used directly
var 对象名 = {属性名1:"属性值1", 属性名2:"属性值2"....}
//不能再去new了