JavaScript Basic episode---Apply,call and URL encoding methods

Source: Internet
Author: User

JS Basic Learning

JS Compression:

is to use the JS syntax format by shortening the variable name, remove the space and so on to compress.

Use of apply and call

1:apply Method: Applies the method of an object to replace the current object with an object. The main change is the value of this.

    1. <script type= "text/javascript" >
    2. ???? var user_name = " fai ";
    3. ???? //A function, here the main notice inside the meaning of this.
    4. ???? function ShowName () {
    5. ???????? Alert (this. user_name);
    6. ????};
    7. ???? var p = {user_name: ' Red '};
    8. ???? Showname.apply (P); //Use the Apply method to change the different of this in the function. This is the p here.
    9. </script>

2:call method: This is the same as the above method, except that a comma is required when a parameter is required, and apply is passed in the form of an array.

This is where parameters are used, and we need to use the form of an array to pass it.

    1. var user_name = " fai ";
    2. A function where the main note is the meaning of this inside.
    3. function ShowName (a,b,c) {
    4. ???? Alert (this. user_name);
    5. ???? Alert (A + B + c);
    6. };
    7. var p = {user_name: ' Red '};
    8. Showname.apply (P); //Use the Apply method to change the different of this in the function. This is the p here.
    9. Showname.apply (P, [12,12,12]);
    10. Showname.call (p,13,13,13);

The above code is the difference between the two. We need to pay attention to these small points of knowledge when we use them.

Recursion for the function:

When the function is an anonymous function, we use recursion when the value is not directly write the assignment of anonymous function, we need to pass Arguments.callee (), to represent the function itself.

    1. var index = 0;
    2. //define anonymous functions
    3. var s = function () {
    4. ???? index++;
    5. ???? alert (index);
    6. ???? if (index<5) {
    7. ???????? //s ();
    8. ???????? Arguments.callee (); //represents the function itself
    9. ????}
    10. };
    11. var ss = S;
    12. ss ();

Encodeurl method: Encode a text string into a valid Uniform Resource Identifier (URL) that does not encode those special characters, and we need to encode the characters using the Encodeurlcompoment method. The range of this code is even wider.

    1. var msg = ' http://localhost:55186/js%e7%bb%83%e4%b9%a0/js11.html ';
    2. var url_value = Window.encodeuri (msg); //String that encodes text only
    3. var sss = window.encodeuricomponent (msg); //coding is more extensive and the preceding numbers are encoded.
    4. Alert (SSS);
    5. alert (Url_value);

Eg: Click on the implementation display to close the Level 2 menu.

Idea: By registering the Click event for the <p> tag, and then finding the <ul> below it, see if it is hidden and in action. {The idea is important, every time the code is that way, only need to learn to solve the problem of the idea, so you can and so on, solve a lot of problems. }

  1. <body>
  2. ???? <ul id= "umenu" >
  3. ???????? <li>
  4. ???????????? <p>111</p>
  5. ???????????? <ul>
  6. ???????????????? <li>1</li>
  7. ???????????????? <li>2</li>
  8. ???????????????? <li>3</li>
  9. ???????????? </ul>
  10. ???????? </li>
  11. ???????? <li>
  12. ???????????? <p>222</p><ul><li>1</li><li>2</li><li>3</li></ul>
  13. ???????? </li>
  14. ???????? <li>
  15. ???????????? <p>333</p><ul><li>1</li><li>2</li><li>3</li></ul>
  16. ???????? </li>
  17. ???? </ul>
  18. </body>

The JS code is as follows:

  1. Window.onload = function () {
  2. ?????? //Register click events for each menu item
  3. ?????? var PS = document.getElementById (' Umenu '). getElementsByTagName (' P ');
  4. ?????? //Traverse PS for each registered click event
  5. ?????? for (var i = 0; i < ps.length; i++) {
  6. ?????????? Ps[i].onclick = function () {
  7. ?????????????? //1; Find the <ul>,+this.parentnode behind P is to find its parent node;
  8. ?????????????? var x = this. Parentnode.getelementsbytagname (' ul ') [0];
  9. ?????????????? //2: Determine if hidden
  10. ?????????????? if (X.style.display! = ' None ') {
  11. ?????????????????? X.style.display = ' None ';
  12. ??????????????} Else {
  13. ?????????????????? X.style.display = ' block ';
  14. ??????????????}
  15. ??????????};
  16. ??????}
  17. ??};

JavaScript Basic episode---Apply,call and URL encoding methods

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.