Chapter 2 notes of the Javascript authoritative guide

Source: Internet
Author: User

1. "|" operator if | the first parameter of the operator is true, or a value that can be converted to trued, the operator returns its first parameter. Otherwise, the second parameter is returned. For example, a = A | []; if a has been defined and is not null, it returns a; otherwise, A is null.

2. Although there are fixed numbers of named parameters when defining JavaScript Functions, the number of parameters passed to this function can be arbitrary. The arguments object allows full access to the actual parameter values. The arguments object is an array-like object.

An example of arguments:

Function B (){

Alert ();

Arguments [0] = [12, 3];

Alert ();

}

B (); // undefined, undefined

B (1); // "1", "12, 3 ";

-------------------------

In this example, we can see that 1. The arguments object is allocated with memory only when parameters are passed in. 2. The arguments object has an extraordinary feature. When a function has a named parameter, the array element of the arguments object is a synonym for the local variables that store the function parameters.

3. callee attributes

The arguments object defines the callee attribute to reference the currently executed function. This attribute is not very useful, but it can be used to recursively call unnamed functions.

Function (x ){

If (x <= 1) return 1;

Return x * arguments. callee (x-1 );

}

4. Apply () and call () Methods: The apply () method has two parameters: this object and an array of parameters to be passed to the function. For example:

 
Function saycolor (sprefix, ssuffix) {alert (sprefix + this. color + ssuffix) ;}; var OBJ = new object (); obj. color = "blue"; saycolor. call (OBJ, new array ("the color is", "a very nice color indeed. "); // The Last generated message is still" the color is blue, a very nice color indeed."

The call () method is the most similar to the classic object impersonating method. Its first parameter is used as the object of this, and other parameters are directly passed to the function itself.
Example:
 
Function saycolor (sprefix, ssuffix) {alert (sprefix + this. color + ssuffix) ;}; var OBJ = new object (); obj. color = "blue"; saycolor. call (OBJ, "the color is", "a very nice color indeed. "); // The Last generated message" the color is blue, a very nice color indeed."

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.