Do you have all the top ten hot javascript pen questions in 2015?

Source: Internet
Author: User

Do you have all the top ten hot javascript pen questions in 2015?

1. Evaluate the knowledge of this 

 
 
  1. var length = 10; 
  2. function fn() { 
  3.   console.log(this.length); 
  4.  
  5. var obj = { 
  6.   length: 5, 
  7.   method: function(fn) { 
  8.     fn(); 
  9.     arguments[0](); 
  10.   } 
  11. }; 
  12.  
  13. obj.method(fn, 1); 

Output: 10 2

Output 10 for the first time should be OK. We know that the object is not only a vertex operator but can also be enclosed in brackets. Therefore, the second execution is equivalent to calling the arguments method. this points to arguments, and two parameters are passed here, therefore, the length of the output arguments is 2.

2. Early declaration of var and functions

 
 
  1. function fn(a) { 
  2.   console.log(a);  
  3.   var a = 2; 
  4.   function a() {} 
  5.   console.log(a);  
  6.  
  7. fn(1); 

Output: function a () {} 2

We know that var and function are declared in advance, and function is declared in priority over var (if both exist). Therefore, the output of a is a function after declaration in advance, then the code executes a to re-assign the value, so the second output is 2.

3. Local and global variables

 
 
  1. var f = true; 
  2. if (f === true) { 
  3.   var a = 10; 
  4.  
  5. function fn() { 
  6.   var b = 20; 
  7.   c = 30; 
  8.  
  9. fn(); 
  10. console.log(a); 
  11. console.log(b); 
  12. console.log(c); 

Output: 10 error 30

This is a mistake I made for a long time. For a long time, I thought {...} the newly declared variable in the function is a local variable. Later, I found that the newly declared variable in the function is a local variable, instead of where the variable declared by var is a global variable. Again, remember that only the newly declared function () {} can be a local variable, while {...}, if {...}, (..) all are global variables (unless they are included in the function ).

4. Implicit variable Declaration

 
 
  1. var f = true; 
  2. if (f === true) { 
  3.   var a = 10; 
  4.  
  5. function fn() { 
  6.   var b = 20; 
  7.   c = 30; 
  8.  
  9. fn(); 
  10. console.log(a); 
  11. console.log(b); 
  12. console.log(c); 

Answer: 10

As I mentioned earlier, function and var will be declared in advance, while variables in {...} will also be declared in advance. So before the code is executed, the variable has been declared, so 'A' in window returns true, and a is assigned a value.

5. Add attributes to basic data. No error is reported, but the value is undefined.

 
 
  1. var a = 10; 
  2. a.pro = 10; 
  3. console.log(a.pro + a); 
  4.  
  5. var s = 'hello'; 
  6. s.pro = 'world'; 
  7. console.log(s.pro + s); 

Answer: NaN undefinedhello

No error is returned when attributes are added to the basic data type, but undefined, 10 + undefined, and NaN are returned if the referenced data type is used. When undefined and string are added, they are converted to strings.

6. function declaration is better than variable Declaration

 
 
  1. console.log(typeof fn); 
  2. function fn() {}; 
  3. var fn; 

Answer: function

Because function declaration is better than variable declaration. We know that before the code is executed line by line, the function declaration and variable declaration will be carried out in advance, and the function declaration will be better than the variable declaration. Here, the better here can be understood as after the variable declaration, if the function name and variable name are the same, the function declaration can overwrite the variable declaration. So the above Code will declare the function and the variable declaration change order is the same result.

7. Identify the most frequently occurring characters in a string and count the number of times

Hash table mode: 

 
 
  1. var s = 'aaabbbcccaaabbbaaa'; 
  2. var obj = {}; 
  3. var maxn = -1; 
  4. var letter; 
  5. for(var i = 0; i < s.length; i++) { 
  6.   if(obj[s[i]]) { 
  7.     obj[s[i]]++; 
  8.     if(obj[s[i]] > maxn) { 
  9.       maxn = obj[s[i]]; 
  10.       letter = s[i]; 
  11.     } 
  12.   } else { 
  13.     obj[s[i]] = 1; 
  14.     if(obj[s[i]] > maxn) { 
  15.       maxn = obj[s[i]]; 
  16.       letter = s[i]; 
  17.     } 
  18.   } 
  19.  
  20. alert(letter + ': ' + maxn); 

Regular Expression mode: 

 
 
  1. var s = 'aaabbbcccaaabbbaaabbbbbbbbbb'; 
  2. var a = s.split(''); 
  3. a.sort(); 
  4. s = a.join(''); 
  5. var pattern = /(\w)\1*/g; 
  6. var ans = s.match(pattern); 
  7. ans.sort(function(a, b) { 
  8.   return a.length < b.length; 
  9. });; 
  10. console.log(ans[0][0] + ': ' + ans[0].length); 

8. Classic Closure

 
 
  1. <! -- Implement a script so that the corresponding link alert is clicked to generate the corresponding number -->
  2. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  3. <Body>
  4. <A href = '#'> first link </a> </br>
  5. <A href = '#'> second link </a> </br>
  6. <A href = '#'> third link </a> </br>
  7. <A href = '#'> fourth link </a> </br>
  8. </Body>

Dom pollution method:

 
 
  1. <! -- Implement a script so that the corresponding link alert is clicked to generate the corresponding number -->
  2. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  3. <Body>
  4. <A href = '#'> first link </a> </br>
  5. <A href = '#'> second link </a> </br>
  6. <A href = '#'> third link </a> </br>
  7. <A href = '#'> fourth link </a> </br>
  8. <Script type = "text/javascript">
  9. Var lis = document. links;
  10. For (var I = 0, length = lis. length; I <length; I ++ ){
  11. Lis [I]. index = I;
  12. Lis [I]. onclick = function (){
  13. Alert (this. index );
  14. };
  15. }
  16. </Script>
  17. </Body>

Closure:

 
 
  1. <! -- Implement a script so that the corresponding link alert is clicked to generate the corresponding number -->
  2. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  3. <Body>
  4. <A href = '#'> first link </a> </br>
  5. <A href = '#'> second link </a> </br>
  6. <A href = '#'> third link </a> </br>
  7. <A href = '#'> fourth link </a> </br>
  8. <Script type = "text/javascript">
  9. Var lis = document. links;
  10. For (var I = 0, length = lis. length; I <length; I ++ ){
  11. (Function (I ){
  12. Lis [I]. onclick = function (){
  13. Alert (I + 1 );
  14. };
  15. }) (I );
  16. }
  17. </Script>
  18. </Body>

9. this

 
 
  1. Function JSClass (){
  2. This. m_Text = 'division element ';
  3. This. m_Element = document. createElement ('div ');
  4. This. m_Element.innerHTML = this. m_Text;
  5. This. m_Element.addEventListener ('click', this. func );
  6. // This. m_Element.onclick = this. func;
  7. }
  8.  
  9. JSClass. prototype. Render = function (){
  10. Document. body. appendChild (this. m_Element );
  11. }
  12.  
  13. JSClass. prototype. func = function (){
  14. Alert (this. m_Text );
  15. };
  16.  
  17. Var jc = new JSClass ();
  18. Jc. Render (); // add div
  19. Jc. func (); // output division element
  20. // Click the added div element division element to output underfined. Why?

Answer: division element undefined

The first output is easy to understand. The second time, I will take a closer look. this actually points to this. m_Element. Because this is the addEventListener function called by this. m_Element, all the internal this points to it. You can try to add a line of code this. m_Element.m_Text = 'Hello world' and alert will output hello world.

10. split

Please write a JavaScript function parseQueryString, which is used to parse the URL parameter into an object, such as: var url = "http://witmax.cn/index.php? Key0 = 0 & key1 = 1 & key2 = 2 ″

 
 
  1. function parseQueryString(url) { 
  2.   var obj = {}; 
  3.   var a = url.split('?'); 
  4.   if(a === 1) return obj; 
  5.   var b = a[1].split('&'); 
  6.   for(var i = 0, length = b.length; i < length; i++) { 
  7.     var c = b[i].split('='); 
  8.     obj[c[0]] = c[1]; 
  9.   } 
  10.   return obj; 
  11. var url = 'http://witmax.cn/index.php?key0=0&key1=1&key2=2'; 
  12. var obj = parseQueryString(url); 
  13. console.log(obj.key0, obj.key1, obj.key2);  // 0 1 2 

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.