The core language features of JavaScript are defined in ECMA-262 in the form of a pseudo-language called ECMAScript.
One, variable and data type 1. Variable
Javasript is a weakly typed language that can define any type of variable through VAR, while the same variable can be assigned to different types.
JavaScript can omit semicolons at the end of a statement, and JS will automatically add semicolons, but preferably with a semicolon : 1. Reduce error 2. Easy Compression 3. Improved performance
var msg = ' mdzz '; // Stringmsg = 1024x768; // Number
var
Declaring variables, function scopes
2. Data type
JavaScript has a total of 5 basic data types, which typeof
can be viewed by the variable's data type. typeof(variable/numeric literal) returns the corresponding data type string
1. Undefined
There is only one value, and the undefined
declaration does not have a variable initialized, and the default value is undefined
. You can also assign values to variables undefined
.
var // x equals var msg = undefined typeof (msg)>> "undefined"
typeof(non-declared variable) also returns "undefined"
2. Null
There is also only one value, for null
, to typeof(null)
return ‘object‘
.
3. Boolean
Two values, true
and a false
. All other types of values can be converted to or from a Boolean (variable) true
false
. Conversion rules:
Data type |
true |
false |
String |
Non-empty string |
Empty string ( ‘‘ ) |
Number |
Non-0 value |
0 AndNaN |
Object |
Any object |
null |
Undefined |
|
undefined |
At some point the variable is automatically converted to a Boolean, such as if (variable) equivalent to if(Boolean (variable) )
4. Number
can represent integers or floating-point numbers. 0 begins with octal, and 0x starts with hexadecimal.
Special values:
Number.MIN_VALUE
(Minimum value
Number.MAX_VALUE
(Maximum value
Infinity
(Infinity, over the JS data range, can be Number.POSITIVE_INFINITY
obtained by
-Infinity
(Negative infinity,Number.NEGATIVE_INFINITY
Number.MAX_SAFE_INTEGER
(Maximum integer
Number.MIN_SAFE_INTEGER
(Minimum integer
NaN
Any NaN
operations involved are returned NaN
, such asNan / 10
NaN
Not equal to any value, including itself,NaN == NaN // false
isNaN()
Used to determine whether a variable is a numeric value
IsNaN (NaN) // trueIsNaN (' ten ') // false can be converted to a numeric value IsNaN (true) // falseIsNaN (' true ') // true
Numeric conversions:
Number()
, parseInt()
, parseFloat()
.
Number()
Conversion rules:
true
and false
will be converted to 1
and0
null
Return0
undefined
ReturnNaN
- The string ignores the leading 0, hexadecimal can be converted, the empty string is 0, and the other illegal string is converted to
NaN
.
- Object, invokes the
valueOf()
method, and then invokes the undefined
string to toString()
convert.
parseInt()
Conversion rules:
parseFloat()
Only decimal is converted.
5. String
Single quotation mark ( ‘
) or double quotation mark ( "
) wrap. There is no difference between the two ways.
Escape characters:
\n
Line break
\t
Tab
\b
Space
\r
Enter
\\
Slash Slash
\‘
Single quotation marks
\"
Double quotes
str.length Get string length
The string in the ECMAScript is immutable. string concatenation will result in a new string.
toString()
Method: In null
addition undefined
to the value, there are methods for other data toString()
. The string returns a copy.
> 0x23. ToString ()< "+" > ' 0x23 '. ToString ()< "0x23"
6. Object
JS can be used var o = new Object()
to create objects.
Object
Each instance has the following properties and methods:
constructor()
: constructors
hasOwnProperty(propertyName)
: Used to check whether a property exists for the current object. Which propertyName
is a string.
isPrototypeOf(Object)
: Used to check if an incoming object is a prototype of another object.
propertyIsEnumerable(propertyName)
: Used to check whether a given property can be enumerated with for-in. Which propertyName
is a string.
toString()
: Returns the string of the object.
valueOf()
: Returns the string, numeric, or Boolean representation of an object.
Two, operator 1. Unary operators
- increment decrement operator
+ +
, --
, for integers, using the same method as the C language, Java, and so on. However, JS can be used not only for integers, but also for floating-point numbers. At the same time, for other types of variables, it is converted to number
and then the operation, and illegal will return NaN
. var o = {valueOf: function () { return -1; }} var i = o--; //
- Unary plus and minus operators are the same for numbers,
+
-
like normal operations symbols, and can be quickly converted to types for other types Number
. var o = { function() { return -1; }} var i =-O; // I was equal to 1, O don ' t change
2. Bitwise operators
- Bitwise NON (
~
): Negative value of the operand minus one.
- Bitwise AND (
&
)
- Bitwise OR (
|
)
- Bitwise XOR OR (
^
)
- Move left (
<<
)
- Arithmetic right Shift (
>>
): Leftmost fill sign bit
- Logical right Shift (
>>>
): the leftmost 0.
3. Boolean operators
- Logical non (
!
): Returns the inverse of the Boolean value. !!
the effect can be achieved by two reverse () Boolean()
.
- Logic and (
&&
): short-circuit operation. If the Boolean value of the first operand is false, the first operand is returned, Note: The operand itself is returned, not the corresponding Boolean value! if the Boolean value corresponding to the first operand is true, the second operand is returned.if NULL ) { obj.dosomething ();} // The above code can be simplified into the following code , obj && obj.dosomething ();
- Logic or (
||
): As with logic, a short-circuit operation.var obj; if NULL { =else { new Object ();} // the code above can be simplified into the following code New Object ();
4. Multiplicative operators
If a multiplicative operation is performed on a non-numeric operand, it is automatically converted to a number.
Over the data range returned Infinity
or -Infinity
;
There is an operand that is NaN
returned NaN
;
Infinity
And 0
multiplied, returned NaN
;
Infinity
And not 0 values, Infinity
multiplied, returned Infinity
;
x/NaN
,,, NaN/x
Infinity/Infinity
x/0
return NaN
.
x/Infinity
, x/0
=NaN
x(有限数值)/Infinity
= x
5. Additive operators
Numeric operations or string operations can be performed.
If one of the operands is a string, the other operand is converted to a string and then concatenated.
Non-numeric operands are converted to corresponding values.
6. Relational operators
- Less than (
<
), greater than (), >
less than or equal to () <=
, greater than or equal to ( >=
)
- A non-numeric operand is converted to a numeric value compared to a numeric value.
- The strings are compared by dictionary order.
7. Equality operators
- Equal (
==
) and unequal ( !=
)
- The two operands do a forced type conversion before comparing their equality. Different data type conversion rules:
-
Boolean
, 0
or1
String
Comparison Number
: String
Number
Object
Comparing other types, the object takes valueOf()
the value returned by the method
null == undefined // true
null == anything // false
null != anything // true
- If the two operands are objects, they are equal when they point to the same object.
- Congruent (
===
) and not congruent ( !==
)
-
- If the two operand data types are not returned equal
false
, the type conversion is not performed.
8. Conditional operator (
? :
) 9. Assignment operator (
=
) 10. Comma operator (
,
): Returns the last item III, statement
Even if there is only one line of code, it is best to { }
enclose it.
if
Statement
do-while()
Statement
while
Statement
for
Statement
The variables defined in the For statement are still valid even after the statement ends, because VAR defines the variable as the function scope.
var count = ten; for (var i = 0; i < count; i++) { // do something ... }alert (i); // Ten
for-in
Statement
The output sequence is variable. Before looping, you should verify that the value of the object is not null
or undefined
.
for inch expression) { statement}
label
Statement
label: statement
break
the specified or continue
location. (same as in Java.)
break
and continue
statements
with
Statement
with
The purpose of the statement is to set the scope of the code to a specific object. It is not allowed in strict mode.
Grammar:with(expression) statement;
var qs = location.search.substring (1); var hostName = location.hostname; var url = location.href; // simplify into the following code with (location) { var qs = search.substring (1); var hostName = hostName; var url = href;}
switch
Statement
Switch (expression) { case value1:statement break; Case value2:statement Break ; Case value3:statement Break ; // Other case default : statement}
Where expression can be of any data type. It needs to be judged.expression === value。
Iv. functions
Basic syntax for a function:
function functionname (arg0, arg1, ..., ArgN) {// function declaration // function call
when you call a function in ECMAScript, the arguments can be different from the number of arguments when the function is declared. The parameters in the ECMAScript are internally represented by an array, which can be accessed by objects within the function body arguments
.
arguments
is not Array
an instance, but can be arguments[index]
obtained by taking any one of the elements. The arguments.length
number of arguments can be obtained by means of a.
We can implement overloading by judging the number of parameters. But there is no function overload in JavaScript itself, and if you define multiple functions with the same name, only the last function will take effect.
function Doadd (NUM1, num2) { if (arguments.length = = 1) {+ ten) ; Else if (Arguments.length = = 2) { alert (arguments[// arguments[0] is equals to NUM1 }}
arguments
and named parameters are synchronous, but the parameters that are not passed can not be changed by changing arguments
. Remember that it arguments
is not an array.
function Doadd (NUM1, num2, num3) { // 2 // 1 2 undefined 1 2 undefined
NUM1 = 3; ARGUMENTS[1] = 4; ARGUMENTS[2] = 5; // 2 // 3 4 undefined 3 4 5 }doadd (1, 2);
JavaScript Advanced Tutorial Learning note I, variables and data types