Zhang xiaoxuan, JavaScript lecture notes 4

Source: Internet
Author: User

1. Comparison of global variables and local variables

<SCRIPT type = "text/JavaScript"> <br/> <! -- <Br/> var MSG = 'Global variable'; <br/> function show () <br/>{< br/> MSG = 'local variable '; // modify the global variable value <br/>}< br/> show (); <br/> alert (MSG ); // The value is a local variable. <br/> // --> <br/> </SCRIPT><SCRIPT type = "text/JavaScript"> <br/> <! -- <Br/> var MSG = 'Global variable'; <br/> function show () <br/>{< br/> var MSG; <br/> MSG = 'local variable '; // only modify the value of the local variable <br/>}< br/> show (); <br/> alert (MSG); // The value is 'Global variable' <br/> // --> <br/> </SCRIPT>

 

2. Functions with variable parameters in Javascript

Use the arguments object in the function to access all parameters passed by the calling program. In the function declaration, the function parameter list is empty.

Eg:

<SCRIPT type = "text/JavaScript"> <br/> <! -- <Br/> function textparams () <br/>{< br/> var Params = ''; <br/> // use the arguments object in the function to access all parameters passed by the calling program. <br/> for (VAR I = 0; I <arguments. length; I ++) <br/>{< br/> Params = Params + ''+ arguments [I]; <br/>}</P> <p> textparams ('abc', 123); <br/> textparams (123,456, 'abc '); <br/>}< br/> // --> <br/> </SCRIPT>

 

3. Create dynamic functions (using function pointers in C/C ++)

VaR varname = new function (argument1,..., lastargument );

Note:

All parameters must be of the string type, and the last parameter must be the function code of this dynamic function.

Eg:

 <SCRIPT type = "text/JavaScript"> <br/> <! -- <Br/> var square = new function ('x', 'y', 'var sum, sum = x * x + y * Y; return sum ;'); </P> <p> alert (square (2, 3); <br/> // --> <br/> </SCRIPT>

What is the use of dynamic functions?

You can dynamically change the function code during program execution;

 

4. Javascript system functions (which can be called directly)

1. encodeuri Method

Returns the result of encoding a URI string, essentially UTF-8 encoding.

Eg:

VaR urlstr = encodeuri ('HTTP: // fanli.qq.com/index.html? A = DDD & MSG = China ');

Alert (urlstr );

 

2. decodeuri Method

Decodes the initial string from URI encoding and returns

 

3. parseint Method

Converts a string to an integer in the specified hexadecimal format.

Parseint (numstring, [Radix]). If the second parameter is not specified, the string prefixed with '0x 'is considered as hexadecimal, and the string prefixed with '0' is considered as octal

(This should be noted that tenfy has planted a heel here), all other strings are treated as decimal. If the string contains a non-numeric string, convert the string with only the preceding digits in hexadecimal notation.

Eg:

Parseint ('123abc', 10); // The result is 123.

 

4. parsefloat method: If the conversion fails, Nan is returned.

 

5. isnan method --- check whether the returned values of parseint and parsefloat methods are Nan (not a number)

Note: This is not allowed to judge whether a number is Nan. If (xx = Nan), you can only use this method to determine whether a number is Nan, for example:

Alert (Nan = Nan); // very strange. The result here is false.
Alert (isnan (NAN); // true

 

6. Escape Method

Returns the encoded result string. All spaces, punctuation marks, accents, and any other ASCII characters are replaced by % XX encoding. XX indicates the hexadecimal format of the Unicode Character Set of the character. About 255 of characters are expressed in % uxxxx format.

 

7. Unescape Method

Decodes the original string from the result string encoded by an escape method and returns it.

 

8. Eval Method

Execute the parameter string as a javascript representation and return the result.

 

 

 

 

 

 

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.