JavaScript design mode-1. Functions

Source: Internet
Author: User
Tags eval

1 <!DOCTYPE HTML>2 <HTML>3     <Head>4         <MetaCharSet= "UTF-8">5         <title>JavaScript advanced Syntax 1-functions</title>6     </Head>7     <Body>8       <Script>9           functionPrint (p) {Ten Console.log (P.tostring (), p) One           } A           /* - var arr= new Array (); - Arr.push (1); the Arr.push (2); - Arr.push (3); - Arr.push (4); - print (arr); + Print (Arr.join (":")) - var arr2 = [5,6,7,8] + Print (Arr.concat (arr2)) A //each () methods for extending functions at Array.each = function (ARRAY,FN) { - For (Var i=0;i<array.length;i++) { - fn (array[i]); -               }     -           } - Array.each (arr,function (v) { in print (v); -           }) to            + //callback function definition - function Add (X,Y,FN) { the this.x=x| | 1; * this.y=y| | 1; $ if (FN) {Panax Notoginseng fn (this.x + this.y); -           } the       } +        A Add (3,5,function (v) { the if (v>5) { + alert ("Res >5") - }else{ $ Alert (res <=5 "") $           } -       }) -       the       - //function-pass parameterWuyi //1. Arbitrary parameter passing (can be passed or transmitted in multiple parameters) the function Add (x, Y, z) { - this.x = x| | 0; Wu This.y = y| | 0; - this.z = z| | 0; About alert (this.x+this.y+this.z); $          } -           -  - Add (12,3,4,5); A //Cons: There is no such thing as high-level languages like Java that have the exact nature of a function.  + //Tip: If you have a class that is a tool class, then the received parameter is preferably an object.  the //conf={gridname: "", Gridcode: "", GridSize: ""} - function Gridutil (conf) { $ alert (conf["Gridname"]+conf["gridSize"]); the          } the Gridutil ({gridname: "Zhangsan", GridSize: "}") the   the //Pass the value or pass the address - var i = +; in var s = "one"; the function Add3 (i,s) { the i++; About s+= "--" the          } the add3 (); the alert (i); + alert (s);//prove that the underlying variable passes the value.  -           the var o = {name: "Zhangdan"};Bayi function Change (o) { the o["name"] = "javascript"; the          } - Change (o); - alert (o.name); proves that the custom object is transmitted by the address.  the           the     the //function recursion the function Add4 (start,end) { - var num= 0; the num + = start; the if (start<end) { the num + = ADD4 (start+1,end);94              } the return num the          } the Alert (ADD4 (1,100));98         About        - //Agent Function--------to determine the new function returned by the program (function of production function)101 //Simulation Database102 var person = {"Jim": ' m ', "Lili": "W"}103 var test = function (name) {104 if (person[name]== "M") { the return function (NK,WK) {106 alert (nk+ "" +wk);107               }108 }else if (person[name]== "W") {109 return function (NK,WK,FK) { the alert (nk+ "+wk+" "+FK");111               } the           }113       } the Test ("Jim") ("OK", "OK"); the Test ("Lili") ("OK", "OK", "no"); the ///front-end code is not commonly used, which complicates the code. Schemas are common. 117       118     */   119        -       //the eval () function--"parses a string into a function and executes121       varStr= "var show = function () {alert ()} ()";122 eval (str);123       //Application Scenario: The database sometimes returns a string (as long as a JS array);124       vara= "[A]"; the       vararr=eval (a);126        for(varI= 0; I<Arr.length;i++){127 Console.log (Arr[i]); -       }129        the      131       </Script> the     </Body>133 </HTML>

JavaScript design mode-1. Functions

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.