JavaScript Advanced Programming (third edition)-3

Source: Internet
Author: User

Equality operators
    • Equality and inequality
      When converting different data types, the equality and not the wait operators follow the basic rules below:
    1. If one of the operands is a Boolean value, convert it to a numeric--false before comparing equality to 0, and true to 1;
    2. 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;
    3. 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:
 
   
  
  1. var result1 = ("55" == 55); // true,因为转换后相等
  2. 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:
 
   
  
  1. var QS = location search substring 1
  2. var HostName = location hostname
  3. 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:

  
 
  1. with(location){
  2. var qs = search.substring(1);
  3. var hostName = hostname;
  4. var url = href;
  5. }

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:

 
   
  
  1. function sum(num1,num2){
  2. return num1 + num2;
  3. alert("Hello world"); // 永远不会执行
  4. }

A function can also contain multiple return statements, for example:

  
 
  1. function diff(num1,num2){
  2. if (num1 < num2) {
  3. return num2 - num1;
  4. } else {
  5. return num1 - num2;
  6. }
  7. }

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.

  
 
    1. function Sayhi ( name message Span class= "pun" >) {
    2. Span class= "PLN" > return ;
    3. alert ( "Hello" Span class= "PLN" > + name + + message //never execute
    4. }< /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:

    1. The function cannot be named eval or arguments;
    2. The parameter cannot be named eval or arguments;
    3. 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:

 
   
  
  1. function Sayhi () {
  2. alert ( + arguments [ 0 ] + + arguments [ 1
  3. }

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.

  
 
  1. function doAdd() {
  2. if(arguments.length == 1) {
  3. alert(arguments[0] + 10);
  4. } else if (arguments.length == 2)
  5. {
  6. alert(arguments[0] + arguments[1]);
  7. }
  8. }
  9. doAdd(10); // 20
  10. doAdd(30,20); // 50

Arguments objects can be used with named parameters, such as:

  
 
  1. function doAdd(num1,num2) {
  2. if(arguments.length == 1) {
  3. alert(num1 + 10);
  4. } else if (arguments.length == 2)
  5. {
  6. alert(arguments[0] + num2);
  7. }
  8. }

The value of arguments is always synchronized with the value of the corresponding named parameter, for example:

  
 
    1. function Doadd ( num1 num2 {
    2. arguments [ 1 ] Span class= "PLN" > = 10
    3. alert ( arguments [ 0 ] Span class= "pun" >+ num2
    4. }

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.
  
 
  1. function addSomeNumber(num){
  2. return num + 100;
  3. }
  4. function addSomeNumber(num){
  5. return num + 200;
  6. }
  7. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.