JavaScript mode Reading Notes Chapter 1 Functions

Source: Internet
Author: User

JavaScript mode Reading Notes Chapter 1 Functions
November 10, 2014
1. JavaScript Functions have two features: a function is the first type of object function that can provide a scope function, that is, an object, as shown in:
-1. functions can be dynamically created at runtime or during program execution.
-2. functions can be assigned to variables. They can be referenced to other variables and can be extended. In addition, functions can be deleted.
-3. It can be passed as a parameter to other functions and can be returned by other functions.
-4. functions can have their own attributes and methods.
2. Declaration VS expression: Name and variable declaration upgraded

// This is a function expression.// It is passed to the CallMe function as a parametercallMe(function(){// This is the name function expression.// Also known as an anonymous Function});// This is the name function expression.callMe(function me(){// Here is the name function expression// And its name is me});// Another function expressionvar myobject = {  say:function(){     // Here is the function expression  }};
3. The function is promoted to the top of the function in the background no matter where the function is declared. 4. Callback Mode
<script> function writeCode(callBack){console.log("callBanck before.....");callBack();console.log("callBanck after....."); }  function callBack(){console.log("callBanck .........."); } writeCode(callBack);  </script>// Output callBanck before ...... test1.html: 14 callBanck ...... test1.html: 21 callBanck after .....
The following shows a complex DOM node query, and then performs business processing in turn in the callback function. The time consumed is 2n.
<script>// Search nodes var findNodes = function(){Var I = 100000, // complex LoopNodes [], // store the running resultFound; // nodes found by the zero-hour storagewhile(i){// Business logic processing//....found = 1;i -= 1;nodes.push(found);}return nodes; };// Process nodes  var hide = function(nodes){var i = 0, max = nodes.length;for(; i < max; i += 1){// Complex business logic processingconsole.log(nodes[i]);} }; // Execute the Function hide(findNodes());  </script>
    <script>var findNodes = function(callBack){var i = 10000, nodes = [],dound;// Check whether the callback function is callableif(typeof callBack != "function"){callBack = false;} while(i){i -= 1; // Logic processing ........ // Run the callback functionif(callBack){callBack(found);}nodes.push[found];}return nodes;}; var hide = function(node){// Perform corresponding business processing on the node}; findNodes(hide);  </script>

5. The closure feature is simply the scope.
6. User-Defined Function-1. If a function is redefined, it overwrites the original function.
     var scareMe = function(){console.log("FIRST!");scareMe = function(){console.log("SECOND!");}; };The output result is as follows:
 
 
Log: FIRST! Log: SECOND!

The pattern is determined. When he redefines himself, any attribute that has been added to the original function will be lost. In addition, if the function uses different names, such as allocating to different variables or using object methods, the redefined part will never happen, in addition, only the original function body of the I system will be used.
  <script> var scareMe = function(){console.log("FIRST!");scareMe = function(){console.log("SECOND!");}; };// Add new attributesscareMe.property = "property";// Assign a value to another variable with different namesvar prank = scareMe; // Use in a methodvar spooky = {boo: scareMe}; prank();// firstprank();//firstconsole.log(prank.property);//property spooky.boo();//firstspooky.boo();//firstconsole.log(spooky.boo.property);//property // Use a custom functionscareMe();//secondscareMe();//secondconsole.log(scareMe.property);//undefined    </script>Output result: FIRST! Test5.html: 14 property test5.html: 332 FIRST! Test5.html: 14 properties test5.html: 372 SECOND! Test5.html: 17 undefined
7. Real-time functions: Execute the syntax of the function immediately after the function is defined. Example:

(function(){   alert("One");}());
This mode is composed of the following parts:-1. You can use a function expression to define a function.
-2. Add a set of parentheses at the end, which causes the function to be executed immediately.
-3. Wrap the entire function in brackets. This mode is generally used to execute page loading tasks during initialization. These variables are not required after initialization. It can completely replace the Code created in the form of a global variable. The following code does not create too many global variables.
<script>     (function(){var days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'],today = new Date(),msg = "Today is " + days[today.getDay()] + "," + today.getDate();alert(msg); }());   </script>

Parameters of Real-Time Functions
 (function(who, how){console.log("i'm " + who + ",I'm " + how); }("pengke", "shabi"));
The return value of the real-time function. The following code points to the return value 4 of the function.
var result = (function(){   return 2 +2 ;})();
8. function properties-the memo mode can cache function parameters. 9. The configuration object applies to a method. We are not sure how many parameters it has. -1. You do not need to remember many parameters and their order. -2. Optional parameters can be safely ignored.
-3: easier reading and Maintenance
-4. It is easier to add or delete parameters.
  <script>function addPerson(conf){console.log(conf.first);console.log(conf.second);console.log(conf);}var conf = {first: "abc",second: "a",third: "c"};addPerson(conf);   </script>
10. Function applications can use Function. prototype. apply ().
  <script>// Define a function var sayHi = function(who){console.log("Hello " + (who ? "," + who : "") + "!"); };sayHi();sayHi('world');sayHi.apply(null, ["world"]);  </script>
Execution resultHello ! test10.html:15Hello ,world! test10.html:15Hello ,world! 
The first parameter of apply is used to specify this in the function. When passing a function, the first parameter cannot be blank and must be a function name. 11. Curry
<script>// Curry add functionfunction add(x, y){var oldx = x, oldy = y;if(typeof oldy == "undefined"){return function(newy){console.log(oldx + newy);};} console.log(x + y);} console.log(typeof add(5));//functionadd(3)(4);//7 var add2000 = add(2000);add2000(10);//2010  </script>












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.