JavaScript Advanced Programming Reading Notes (3) (Chapter 3)

Keywords: ECMAScript less Java Javascript

Process statement:

Some unusual process statements are listed:

1. do-while statement:

Before evaluating conditional expressions, the code in the loop is executed at least once:

var i = 0;
do {
i += 2;
} while (i < 10);
alert(i); ==>Eject10

2. for-in statement:

The for-in statement is a precise iteration statement that can be used to enumerate the attributes of an object.
Example: Use the for-in loop to display all the properties of window objects in BOM:

for (var propName in window) {
    document.write(propName+"<br/>");
}

If the variable value representing the object to iterate is null or undefined, the for-in statement throws an error. ECMAScript 5 corrects this behavior; instead of throwing errors in this case, it simply does not execute loops. To ensure maximum compatibility, it is recommended that the value of the object be checked to confirm that it is not null or undefined before using the for-in loop.

3. break and continue statements:

The break statement ends the loop directly, while the continue statement skips the current loop from the top of the loop
The Ministry continued to implement.

4. switch statement

The switch statement is more efficient than the if statement.
Syntax:

switch (expression) {
    case value: statement
    break;
    case value: statement
    break;
    case value: statement
    break;
    case value: statement
    break;
    default: statement
}

By adding a break statement after each case, you can avoid executing multiple case codes at the same time. If you do need to mix several situations, don't forget to add comments to your code to indicate that you intentionally omitted the break keyword:

switch (i) {
    case 25:
    case 35:    /* Merge two situations */
    alert("25 or 35");
    break;
    case 45:
    alert("45");
    break;
    default:
    alert("Other");
}

Any data type, whether string or object, can be used in the switch statement. Secondly, the value of each case is not necessarily a constant, it can be a variable or even an expression:

//Example 1:
switch ("hello world") {
    case "hello" + " world":
    alert("Greeting was found.");
    break;
    case "goodbye":
    alert("Closing was found.");
    break;
    default:
    alert("Unexpected message was found.");
}

//Example two:
var num = 25;
switch (true) {
    case num < 0:
    alert("Less than 0.");
    break;
    case num >= 0 && num <= 10:
    alert("Between 0 and 10.");
    break;
    case num > 10 && num <= 20:
    alert("Between 10 and 20.");
    break;
    default:
    alert("More than 20.");
}

function

Parameters:

The ECMAScript function does not care how many parameters are passed in or what data type the parameters are. That is to say, even if you define a function that only takes two parameters, you don't have to pass two parameters when calling the function. One, three or even no parameters can be passed:

function sayHello(){
    alert("Hello "+arguments[0] + arguments[1]);
}
sayHello();//Hello undefiend undefiend
sayHello('java','script');//Hello javascript

This example illustrates an important feature of ECMAScript functions: named parameters are convenient, but not necessary.
The parameters in ECMAScript are represented internally by an array, arguments.
Functions can be developed according to this feature of ECMAScript function parameters:

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

The arguments object can be used with named parameters:

function doAdd(num1, num2) {
    if(arguments.length == 1) {
    alert(num1 + 10);
    } else if (arguments.length == 2) {
    alert(arguments[0] + num2);//First parameter plus second parameter
    }
}
doAdd(10,20);//30

Be careful:
The length of an arguments object is determined by the number of parameters passed in, not by the number of named parameters when defining a function. Named parameters that do not pass values are automatically assigned undefined values.

No overload:

ECMAScript functions cannot be overloaded in the traditional sense.
If two functions with the same name are defined in ECMAScript, then the name belongs only to the post-defined function, that is to say, only the latter function is executed in sequence. However, by examining the type and number of parameters in the incoming function and reacting differently, the overload of the method can be imitated.

Posted by Fusioned on Mon, 25 Mar 2019 21:06:28 -0700