Javascipt-Basic essay

Source: Internet
Author: User

Javascip-basic--- Notes for details:

1. Special values: Nan, infinity, isnan (), isfinite ()

NAN:

 
VaRA = parseint ('a123'); Window. Alert ();//Output Nan

Infinity:

 
Window. Alert (6/0); // output infinity (it is better not to write this)

Isnan (): determines whether it is a number. If it is not a number, true is returned. If it is a number, false is returned.

 
VaRA = "DD"; Window. Alert (isnan ());//Returns true.

Isfinite ():Used to determine whether it is infinite.If number is Nan (not a number) or positive or negative infinity, false is returned.

 
Window. Alert (isfinite (6/1); // returns truewindow. Alert (isfinite (6/0); // returns false

2. logical operators:

In logical operations,0,"",False,Null,Undefined,NanBoth representFalse

(Or |)| Returns the value of the first value not false (the object can also be), or the last value (if all values are false)

This knowledge point is widely used in Javascript frameworks.

A,

VaRA =True;VaRB =False;VaRC = B |A; window. Alert (C );//Output True

B,

 
VaRA = 2;VaRB = 0VaRC = A |B; window. Alert (C );//Returns the first value and outputs 2.

C,

VaRA =False;VaRB ="";VaRC = 0;VaRD =NewObject ();//ObjectVaRAA = A | B | c | D;//A, B, and C are all false. This Returns D.Window. Alert (AA );//Returns D (object)

4. multi-branch Switch

  var  flag = 1 ;   switch   (FLAG) {  default  : window. alert ( "none of them" );   case  'A' : window. alert ( "A" );   case  'B' : window. alert ( "B"); ///   no break statement, no match is successful, and all results are output  } 
VaRFlag = 1;Switch(FLAG ){Default: Window. Alert ("Nothing");Case'A': Window. Alert ("");Case1: Window. Alert ("B ");// No break statement. If the match is successful, the break statement is no longer found. At this time, B is output.}

5. function call

Func. js

FunctionABC (VAL) {window. Alert ("ABC ()" +Val );}//Functions with return valuesFunctionTest (num1, num2 ){VaRRes = 0; Res= Num1 +Num2;ReturnRes ;}//Functions without return valuesFunctionNoval (num1, num2 ){VaRRes = 0; Res= Num1 +Num2 ;}

Function call:

<HTML>  <SCRIPT type = "text/JavaScript" src = "func. js"> </SCRIPT> <SCRIPT type = "text/JavaScript"> //  Function call 1 --- common call (common call function) ABC (50 );  // Function call 2 ---  Variable = function nameWhen calling:Variable (actual parameter)      VaR Test1 = ABC; //  In this case, the variable is equivalent to the reference (pointer) of the function) Window. Alert (ABC ); // Output the entire ABC FunctionCode, You can understand Test1 (500 );  //  If the called function has a return value, you canProgramIs returned directly, no return value is returned, but you have received it, this is returned undefined      //  Call a function with a returned value      VaR Res = test (20, 40 ); Window. Alert (RES );  //  Call a function without a return value Window. Alert ("calling a function without return values" );  VaR Res = noval (1, 1);  //  Output undefined  Window. Alert (RES ); </SCRIPT> 

JS supports functions with Variable Parameter count

<HTML>  //  Function call-recursion  /*  Function ABC (Num) {If (Num> 3) {ABC (-- num);} document. writeln (Num);} // call the function ABC (5); // output 3 3 4 */      //  JS supports functions with Variable Parameter count      Function  ABC (){  //  JS provides an arguments that can access the input value. Window. Alert (arguments. Length ); //  Number of incoming records          //  Traverse input parameters          For ( VaR I = 0; I <arguments. length; I ++ ) {Window. Alert (arguments [I]) ;}} //  Call Window. Alert ("ABC (12,13, \" Hello \ ", 56 )" ); ABC ( 12, 13, "hello", 56 ) Window. Alert ( "ABC (5 )" ); ABC ( 5 ); Window. Alert ( "ABC ()" ); ABC (); </SCRIPT> 

Use global variables and local variables in the function

<SCRIPT type = "text/JavaScript">//Global variables and local variablesVaRNum = 90;FunctionTest (){//In a function, if VaR is not included, global variables are used,//If VaR is included, a new num variable is defined in the function.//VaR num = 900;Num = 900;} Test (); window. Alert ("Num =" + num );//Output num = 900.</SCRIPT>

6. There are two methods to access object attributes

<SCRIPT type = "text/JavaScript">/*  // In JS, everything is the object function person () {} window. alert (person. constructor); var A = new person (); window. alert (. constructor); window. alert (typeof A); window. alert (person );  */      //  There are two ways to access object attributes: 1. Object Name. attribute name 2. Object Name ["attribute name"] (dynamic call)      Function  Person (){}  VaR A = New  Person () A. Name = "James" ; Window. Alert (A. Name );  // Normal Mode      VaR STR = "Na" + "me" ; Window. Alert (A [STR]);  //  Dynamic Mode      //  Window. Alert (A ["name"]); // output James </SCRIPT>

7,There are five ways to create an object

    1. Factory method-use new object to create an object and add relevant properties.
    2. Use constructors to define classes (prototype objects ).
    3. Use prototype
    4. Constructor and prototype hybrid mode.
    5. Dynamic prototype.

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.