JS Object-Oriented Learning Note VII (function and basic control structure)

Source: Internet
Author: User

"Function section"

1. Function built-in parameter array object arguments

function sum () {    var len = arguments.length;     var s = 0;     for (var i = 0;i<len;i++) {         s + = Arguments[i];    }     return s;}; var b = sum (1,2,3,4,5); alert (b); 15


2. Pre-defined built-in functions

parseint ()                 //String-to-integer type   The second parameter selects  the binary parseint (0777,8);p arsefloat ()/               /string to float type isNaN ()                    //To determine if it is not a number Not return Trueisfinite ()                 //Detect a non-Nan number that is not infinite or infinitesimal  returns Trueencodeuri ()                //url encoding available partial escape only escape parameter decodeURI ()                // URL decoding encodeuricomponent ()       //url encoding is not fully escaped all symbols are escaped decodeuricomponent ()       //url decoding eval ()                     //Parameters as JavaScript code performs slow performance unsafe alert ()                    //pop-up warning box  will block thread


3. variable extension type----function

var sum = function (A, b) {return a+b;}; var add = sum; Add (+); 3

4. function as parameter

function Add (a) {    return a+1;   }  function Addachd (a,b,func) {    return a+b+func (b);}  The function is passed as a parameter alert (ADDACHD (4,5,add)); 15//We can also use the function alert (Addachd (4,5,function (c) {return c+1;}) that can be reduced by a global declaration; 15

5. Self-tuning function

  The first parenthesis is the anonymous function itself  //The first parenthesis is placed inside the parameters of the anonymous function and immediately called  (function (a) {     alert (a+1);  }) (5);

6. Closures



In essence, the function B inside a function A does not use the variable a var B, which is internal to a. function A () {    var a = 9;    B = function () {        return A;    }    return B;     } Alert (B ());/*===================================================*/var result=[];  function foo () {      var i= 0;         for (; i<3;i=i+1) {            result[i]=function () {                alert (i);}}}    ; foo ();   Result[0] (); Closures are not assigned at the initial time and will be searched for corresponding values when executed result[1] ();  RESULT[2] ();  

7. Private Functions

/*===================================================*/function A () {    var a = 9;    B = function () {        alert (a);    }} alert (A ());//Any function has a default return value undefined function a () {    var a = 9;    var B = function () {        alert (a);       All child functions can call any variable declared by the parent function global function can not be closed packet can    }    B ();} A (); 9

/*===================================================================================*/

"Basic Control Structure"

"If/else" if (...) {    //code here ...} else{    //code here ...} "Switch/case" var a = 1; Switch (a) {case    1:      //code      ... break;    Case 2:      //code      ... break;    ...    Default:      //code      here ... Break } "while"  var i = 1, while (i<10) {    i++;} "Do." While "  var i = 1; do{   i++;} while (i<10) ' for ' for (var i = 0; i<10; i++) {     //code ...} "For...in" (typically used for array traversal) var arr = [1,2,3,4,5]; for (Var j in arr) {    //code    ... Alert (j+ "value:" +arr[j]);}


.





JS Object-Oriented Learning Note VII (function and basic control structure)

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.