Javascript-1. event + 2. Variable + 3. Variable initialization + 4. function declaration + 5. Anonymous Function

Source: Internet
Author: User
1. Events

In the hyperlink click, execute javascript:<A Href= "Javascript: Alert ('haha ')">Haha</A>

The event concept is also available in Javascript. It can also be executed when a button is clicked.
Javascript:
• <Input type = "button" onclick = "alert (99)" value = "long time"/>
• "Javascript:" is required only for the Javascript in the href of the hyperlink, because it is not an event, but "" javascript: "is regarded as" http: "and" ftp: "," Thunder: // "," ed2k: // ", and" mailto: "are handled by the JS parsing engine. Only href. This is a special case.

 

2. Variables

(1) A string can be declared using double quotation marks in JavaScript,You can also use single quotes to declare strings.. It is mainly used to facilitate HTML integration and avoid the trouble of escape characters.

(2) In JavaScriptNull,UndefinedNull indicates that the value of the variable is null, and undefined indicates that the variable has not pointed to any object and has not been initialized. Both
.

(3) JavaScript is of a weak type. When declaring a variable, it cannot: int I = 0; it can only be declared through var I = 0; it is different from VAR in C, not the type in C #
Inference.

(4) In JavaScript, variables can also be declared without VaR and used directly. Such variables are "global variables", so it is best to use them unless you really want to use global variables.
Add var.

(5) JS is dynamic, soVaR I = 0; I = "ABC"; is valid.

 

 

3. Judge variable Initialization Hide row number CopyCode ? This is Program Code.
  1. <ScriptType= "Text/JavaScript">
  2. // Alert (new date (). tolocaledatestring ());
  3. VaRX = 0;
  4. If(X =Null){
  5. Alert ("Null");
  6. }
  7. If(Typeof(X) ="Undefined"){
  8. Alert ('Undefined');
  9. }
  10. If(! X) {alert ('No x');}
  11. </Script>

If (x) {} // The variable is initialized, the variable is not empty, or the variable is not 0. The last method is recommended.

 

4. function declaration

(1) how to declare a function in javascript:

Function add (I1, I2 ){
Return I1 + I2;

}

Int add (INT I1, int I2) // C # statement

(2)The Return Value Type and parameter type do not need to be declared.. Function Definition starts with function.

VaR r = add (1, 2 );

Alert (R );

R = add ("hello", "Tom ");

Alert (R );

(3) JavaScript does not require that all paths have return values as in C #. If no return value is returned, it is undefined.

(4) Easy to error: Do not use a custom function name with the built-in JS or DOM methods, such as selectall or focus.

 

5. Anonymous Functions

VaR F1 =Function(I1, I2 ){

ReturnI1 + I2;

}

Alert (F1 (1, 2 ));


(1) It is similar to the anonymous function in C.

(2) This anonymous function is used in many jquery functions.

(3) Alert (Function(I1, I2 ){ReturnI1 + I2 ;}(); // directly declare an anonymous function and use it immediately. Using anonymous functions saves you the need to define a function that is not used once, and avoids naming conflicts. js does not have the namespace concept, so it is easy to conflict function names. The example shows that the final declaration is correct once a name conflict occurs.

(4) It must be <SCRIPT src = "my1.js" type = "text/JavaScript"> </SCRIPT> and cannot: <SCRIPT src = "my1.js" type = "text/JavaScript"/>

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.