Case
Variable, function name, operator case-sensitive
Identifier
Variables, functions, attribute names, function parameters, the first character must be a character, an underscore (_), or dollar sign ($)
Other characters can be letters, underscores, dollar signs or numbers
Note
// 单行注释
/*
* 多行注释
*
*/
Strict Mode
ECMAScript 5 strict mode to introduce the concept to define a different JavaScript parsing and execution model
In strict mode, some uncertainties in the behavior of ECMAScript 3 are addressed, but also for some unsafe operations will throw an error.
Enable strict mode throughout the script, add at the top
"use stricr"
You can also specify a function to perform in strict mode
function doSomething() {
"use stricr";
// 函数体
}
Statement
Not forced to use a semicolon as the end of the statement, but do not recommend omitting title
var sum = a + b // 没有分页也是有效语句,不推荐
var diff = a - b; // 有效语句,推荐
Recommended use {}
braces to combine a plurality of statements to the code block
if (test)
alert(test); // 有效但容易出错,不要使用
if (test) { // 推荐使用
alert(test);
}
Keywords and reserved words
Reference ECMA-262
variable
ECMAScript variable support loosely bound
Definition of variables var
operator, e.g.
var message;
var sum = 10;
var str = "hi";
Note: var
the variable will be the operator of the local variables defined in the definition of the variable scope
function test() {
var message = "hi"
}
test();
console.log(message); // 错误
Although not var
operator can define global variables, but does not recommend this practice, because it is difficult to maintain, resulting in confusing code
function test() {
message = "hi"
}
test();
console.log(message); // "hi"
You can use a statement to define a number of variables
var message = "hi",
str = "hello",
age = 29;
type of data
5 kinds of simple data types (basic data types)
- Undefined
- Null
- Boolean
- Number
- String
One kind of complex data types
- Object
typeof
Operators
Use of a value typeof
operator may return one of the following string
Undefined
If this value is not definedboolean
If this value is a Boolean valuestring
If this value is a stringnumber
- If this value is the valueobject
If this value is null or an objectfunction
- If this value is a function of
var message = "some string";
console.log(typeof message); // string
console.log(typeof (message)); // string
console.log(typeof 95); // number
function f() {
// do something
}
console.log(typeof f); // function
var result = true;
console.log(typeof result); // boolean
var value;
console.log(typeof value); // undefined
Note: typeof
it is an operator, not a function of the code more kinds may be used parentheses, but not necessary
Undefined type
Undefined only one type of undefined
special value in use var
when you declare a variable to be initialized but it, this variable is undefined is
var message;
console.log(message == undefined); // true
var message = undefined; // 没有必要显式地将一个变量的值设置为undefined
console.log(message == undefined); // true
Null Type
Null only one type of null
special value, null value representing a null object pointer
var car = null;
console.log(typeof car); // "object"
Although the following code output true
console.log(null == undefined); // true
There is no need to explicitly set the value of a variable is undefined, but this rule does not apply to null, as long as the variable is intended to hold the object has not really save the object, you should definitely make the variable holds the null value,
Number Type
Number type represents the integer and floating point
It can be expressed in decimal, octal, hexadecimal numbers
Octal literal must first bit is 0, then the sequence of octal digits 0 to 7. If the value is out of range thereof literal, then the previous 0 are ignored, as the number after the decimal value analysis. Octal number in strict mode is invalid, and will throw an error
var num = 070; // 八进制的56
var n = 079; // 无效的八进制数字,解析为79
The first two hexadecimal literals must be 0x, followed by any hexadecimal numbers (0 to 9, and A ~ F), A ~ F case insensitive
var a = 0xa; // 十六进制的10
var b = 0x1F; // 十六进制的31
String type
string type is used to represent zero or more 16-bit Unicode character sequences of characters, i.e., the string may be represented by a double quote ( ") or a single quote ( ')
var str = "Hello, Wrold!";
var str = '你好, 世界!';
Characterized string length immutable
Converted to a string, using the toString()
method
var a = 11;
var str = a.toString();
console.log(a + 1) // 12
console.log(str + 1); // 111
You can use toString()
methods hex conversion
var num = 10;
console.log(num.toString()); //10
console.log(num.toString(2)); //1010
console.log(num.toString(8)); //12
console.log(num.toString(10)); //10
console.log(num.toString(16)); //a
Object Types
A collection of data and functionality
var o = new Object();
var a = new Object; // 有效,但不建议省略圆括号
Each Object instance has the following attributes and methods
constructor
Save the function used to create the current objecthasOwnProperty(propertyName)
Checking for a given attribute in the current instance of the presence or absence of speciesisPropertyOf(propertyName)
Whether the object is a prototype for checking incoming current objectpropertyIsEnumerablr(propertyName)
It used to check whether a given attribute can use for-in statement to enumeratetoLocaleString()
Returns a string representation of the object, the region corresponding to the execution environment of the stringtoString()
Returns a string representation of the objectvalueOf()
Returns the string object