Equality operators
- Equality and inequality
When converting different data types, the equality and not the wait operators follow the basic rules below:
- If one of the operands is a Boolean value, convert it to a numeric--false before comparing equality to 0, and true to 1;
- If one operand is a string and the other operand is a numeric value, the string is converted to a numeric value before equality is compared;
- If one operand is an object and the other operand is not, the valueof () method of the object is called, and the resulting base type value is compared with the preceding rule.
The two operators follow the following rules when comparing them:
- null and undefined are equal.
- Before you can compare equality, you cannot convert null and undefined to any other only.
- If one of the operands is Nan, the equality operator returns FALSE, and the inequality operator returns TRUE. even though the two operands are Nan, the equality operator also returns false because Nan is not equal to Nan, according to the rule.
- If the two operands are objects, then the comparison is not the same object. The equality operator returns TRUE if all two operands point to the same object, otherwise, false is returned.
- Congruent and non-congruent
The equality operator is represented by 3 equals good (= = =), which returns true only if the two operands are equal without conversion, as follows:
var result1 = ("55" == 55); // true,因为转换后相等
var result2 = ("55" === 55); // false,因为不同数据类型不相等
Statement
The
- for-in statement
For-in statement is a precise iteration statement that you can use to enumerate the properties of an object. The syntax for the for-in statement:
for properties in expression statement
ECMAScript objects have no order, so The order of the property names that are output through the for-in loop is unpredictable, and all properties are returned once, but the priority returned may vary by browser. The purpose of the
- with statement
With statement is to set the scope of the code to a specific object. Syntax for the WITH statement:
with (expression) statement;
The purpose of the
definition with statement is primarily to simplify the work of writing the same object multiple times, as in the following example:
var QS = location search substring 1
var HostName = location hostname
var URL = location href
The previous lines of code contain the Location object. If you use the WITH statement, you can rewrite the above code into the following form:
with(location){
var qs = search.substring(1);
var hostName = hostname;
var url = href;
}
This means that within the code base of the WITH statement, each variable is first considered a local variable, and if the definition of the variable is not found in the local variable, the location object is queried for the property with the same name, and if a property of the same name is found, the value of the Location object property is the value of the variable.
The WITH statement is not allowed in strict mode, otherwise it is considered a syntax error.
The use of the WITH statement in an orderly manner can cause performance degradation and also make debugging code difficult, so it is not recommended to use the WITH statement when developing large applications.
Function
A function in ECMAScript does not have to specify whether to return a value when it is defined. The function can implement the return value at any time by the return statement followed by the value to be undone. Any code that is behind the return statement will never be executed, for example:
function sum(num1,num2){
return num1 + num2;
alert("Hello world"); // 永远不会执行
}
A function can also contain multiple return statements, for example:
function diff(num1,num2){
if (num1 < num2) {
return num2 - num1;
} else {
return num1 - num2;
}
}
This function is used to calculate the difference of two values.
Also, the return statement can have no return value. In this case, the function returns a undefined value after it has stopped executing. This practice is typically used in situations where you need to stop the function execution prematurely without requiring a return value.
-
function Sayhi ( name message Span class= "pun" >) {
-
Span class= "PLN" > return ;
-
alert ( "Hello" Span class= "PLN" > + name + + message //never execute
-
}< /span>
the recommended practice is either to have the function always return a value, or never to return a value.
Some limitations of strict mode on functions:
- The function cannot be named eval or arguments;
- The parameter cannot be named eval or arguments;
Two named arguments cannot appear with the same name.
If this occurs, a syntax error is caused.
Understanding Parameters:
The ECMAScript function does not mind how many arguments are passed, nor does it matter what data type the parameter is passing in. The reason is that the parameters in ECMAScript are represented internally using an array. The function always receives this array, regardless of which parameters are included in the array (if there are arguments). In fact, in the body of the function, you can access the parameter array through the arguments object to get each parameter passed to the function.
The arguments object is just like an array (it is not an instance of an array) because you can use square brackets to position each of its elements, using the Length property to determine how many arguments are passed in.
In the previous example, the first parameter of the Sayhi () function is named name, and the value of the parameter can be obtained by accessing argument[0]. Therefore, the function can be rewritten as:
-
function Sayhi () {
-
alert ( + arguments [ 0 ] + + arguments [ 1
-
}
This example reflects an important feature of the ECMAScript function: named parameters are only convenient, but not required.
By accessing the length property of the arguments object, you can tell how many parameters are passed to the function, which allows the function to receive one parameter and to implement the appropriate function separately.
function doAdd() {
if(arguments.length == 1) {
alert(arguments[0] + 10);
} else if (arguments.length == 2)
{
alert(arguments[0] + arguments[1]);
}
}
doAdd(10); // 20
doAdd(30,20); // 50
Arguments objects can be used with named parameters, such as:
function doAdd(num1,num2) {
if(arguments.length == 1) {
alert(num1 + 10);
} else if (arguments.length == 2)
{
alert(arguments[0] + num2);
}
}
The value of arguments is always synchronized with the value of the corresponding named parameter, for example:
-
function Doadd ( num1 num2 {
-
arguments [ 1 ] Span class= "PLN" > = 10
-
alert ( arguments [ 0 ] Span class= "pun" >+ num2
-
}
Each execution of the Doadd () function overrides the second parameter, changing it to 10. However, this effect is unidirectional, and modifying a named parameter does not change the corresponding value in arguments. If only one parameter is passed, the value set for ARGUMENTS[1] is not reflected in the named parameter.
Named parameters that do not pass a value are automatically assigned the undefined value. For example, if you pass only one argument to the Doadd () function, the undefined value is automatically saved in num2.
In strict mode, it is not possible to assign a value as in the previous example, which means that even if you set arguments[1] to 10,num2, the value is still undefined. Overriding the value of arguments causes a syntax error (the code will not be executed).
All parameters in the ECMAScript are passed as values, and arguments cannot be passed by reference.
- ECMAScript function is not overloaded
If two functions with the same name are defined in ECMAScript, the name belongs only to the post-defined function.
function addSomeNumber(num){
return num + 100;
}
function addSomeNumber(num){
return num + 200;
}
var result = addSomeNumber(100); // 300
As mentioned earlier, you can mimic the overloads of a method by examining the type and number of parameters in the incoming function and reacting differently.
The functions in ECMAScript are different from functions in other languages:
- You do not need to specify a function's return value, because any ECMAScript function can return any value at any time.
- In fact, a function that does not specify a return value returns a special undefined value.
- There is also no concept of a function signature in ECMAScript, because its function parameters are passed in a form of an array of 0 or more values.
- You can pass any number of arguments to the ECMAScript function, and you can access these parameters through the arguments object.
- The ECMAScript function cannot be overloaded because there are no attributes for function signatures.
From for notes (Wiz)
JavaScript Advanced Programming (third edition)-3