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.
- <script type= "text/javascript" >
- ???? var user_name = " fai ";
- ???? //A function, here the main notice inside the meaning of this.
- ???? function ShowName () {
- ???????? Alert (this. user_name);
- ????};
- ???? var p = {user_name: ' Red '};
- ???? Showname.apply (P); //Use the Apply method to change the different of this in the function. This is the p here.
- </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.
- var user_name = " fai ";
- A function where the main note is the meaning of this inside.
- function ShowName (a,b,c) {
- ???? Alert (this. user_name);
- ???? Alert (A + B + c);
- };
- var p = {user_name: ' Red '};
- Showname.apply (P); //Use the Apply method to change the different of this in the function. This is the p here.
- Showname.apply (P, [12,12,12]);
- 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.
- var index = 0;
- //define anonymous functions
- var s = function () {
- ???? index++;
- ???? alert (index);
- ???? if (index<5) {
- ???????? //s ();
- ???????? Arguments.callee (); //represents the function itself
- ????}
- };
- var ss = S;
- 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.
- var msg = ' http://localhost:55186/js%e7%bb%83%e4%b9%a0/js11.html ';
- var url_value = Window.encodeuri (msg); //String that encodes text only
- var sss = window.encodeuricomponent (msg); //coding is more extensive and the preceding numbers are encoded.
- Alert (SSS);
- 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. }
- <body>
- ???? <ul id= "umenu" >
- ???????? <li>
- ???????????? <p>111</p>
- ???????????? <ul>
- ???????????????? <li>1</li>
- ???????????????? <li>2</li>
- ???????????????? <li>3</li>
- ???????????? </ul>
- ???????? </li>
- ???????? <li>
- ???????????? <p>222</p><ul><li>1</li><li>2</li><li>3</li></ul>
- ???????? </li>
- ???????? <li>
- ???????????? <p>333</p><ul><li>1</li><li>2</li><li>3</li></ul>
- ???????? </li>
- ???? </ul>
- </body>
The JS code is as follows:
- Window.onload = function () {
- ?????? //Register click events for each menu item
- ?????? var PS = document.getElementById (' Umenu '). getElementsByTagName (' P ');
- ?????? //Traverse PS for each registered click event
- ?????? for (var i = 0; i < ps.length; i++) {
- ?????????? Ps[i].onclick = function () {
- ?????????????? //1; Find the <ul>,+this.parentnode behind P is to find its parent node;
- ?????????????? var x = this. Parentnode.getelementsbytagname (' ul ') [0];
- ?????????????? //2: Determine if hidden
- ?????????????? if (X.style.display! = ' None ') {
- ?????????????????? X.style.display = ' None ';
- ??????????????} Else {
- ?????????????????? X.style.display = ' block ';
- ??????????????}
- ??????????};
- ??????}
- ??};
JavaScript Basic episode---Apply,call and URL encoding methods