1. Visit this
var length = 10;function fn () { console.log (this.length);} var obj = {length:5, method:function (FN) { fn (); Arguments[0] ();
Output: 10 2
The first output 10 should be no problem. We know that the object belongs to the exception of the point operator can also use brackets, so the second execution is equivalent to arguments call method, this point to arguments, and here passed two parameters, so the output arguments length of 2.
2. Advance declaration of Var and functions
function fn (a) { console.log (a); var a = 2; function A () {} Console.log (a);} fn (1);
Output: Function A () {} 2
We know that Var and function are declared in advance, and that function takes precedence over the Var declaration (if it exists at the same time), so the pre-declared output of a is a function, and then the code goes down a to re-assign the value, so the second output is 2.
3. Local variables and global variables
var f = true;if (f = = true) { var a = 10;} function fn () { var b =; c = 30;} fn (); Console.log (a); Console.log (b); Console.log (c);
Output: 10 Error 30
It's a mistake I've made for a long time and I've been thinking about it for a while ...} The newly declared variable inside is a local variable, and later I find that the new declared variable within the function is the local variable, and the variable that is not declared with VAR is the global variable. Again, remember that only the new declaration within function () {} is a local variable, while{...}, if{...}, and for (..) is a global variable (unless it is contained within a function).
4. Implicit declaration of variables
if (' A ' in window) { var a = 10;} alert (a);
Answer: 10
I said before that function and VAR will be declared in advance, and in fact {...} Variables are also declared in advance. So before the code is executed, the A variable is declared, and the ' a ' in window returns True,a is assigned.
5, to the basic type of data to add attributes, no error, but when the value is undefined
var a = 10;a.pro = 10;console.log (A.pro + a); var s = ' Hello '; s.pro = ' World '; Console.log (S.pro + s);
Answer:NaN Undefinedhello
Adding attributes to the base type data does not give an error, but the reference returns undefined,10+undefined return Nan, while undefined and string are added to the string.
6. Function declaration is better than variable declaration
Console.log (typeof fn); function fn () {};VAR fn;
Answer: function
Because function declarations are better than variable declarations. We know that function declarations and variable declarations advance ahead of line execution, and function declarations are better than variable declarations, where better than a variable declaration, a function declaration can override a variable declaration if it is the same as the variable name. So the above code will have the same result as the function declaration and the variable declaration order.
7. Determine the most frequently occurring characters in a string, and count the number of times
var s = ' aaabbbcccaaabbbaaa '; var obj = {};var MAXN = -1;var Letter;for (var i = 0; i < s.length; i++) { if (Obj[s[i]) ) { obj[s[i]]++; if (Obj[s[i]] > Maxn) { MAXN = obj[s[i]]; letter = S[i]; } } else { Obj[s[i]] = 1; if (Obj[s[i]] > Maxn) { MAXN = obj[s[i]]; letter = S[i];}} Alert (letter + ': ' + MAXN);
var s = ' aaabbbcccaaabbbaaabbbbbbbbbb '; var a = S.split ('); A.sort (); s = A.join ("); var pattern =/(\w) \1*/g;var ans = s.ma TCH (pattern); Ans.sort (function (A, b) { return a.length < b.length;});; Console.log (Ans[0][0] + ': ' + ans[0].length);
8. Classic Closures
<!--implement a script so that clicking on the corresponding link alert out the corresponding number--><meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/><body> <a href= ' # ' > First link </a> </br> <a href= ' # ' > Second link < /a> </br> <a href= ' # ' > Third link </a> </br> <a href= ' # ' > Fourth link </a> </br& Gt;</body>
<!--implement a script so that clicking on the corresponding link alert out the corresponding number--><meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/><body> <a href= ' # ' > First link </a> </br> <a href= ' # ' > Second link < /a> </br> <a href= ' # ' > Third link </a> </br> <a href= ' # ' > Fourth link </a> </br& gt; <script type= "Text/javascript" > var lis = document.links; for (var i = 0, length = lis.length; i < length; i++) { lis[i].index = i; Lis[i].onclick = function () { alert (this.index); }; } </script></body>
<!--implement a script so that clicking on the corresponding link alert out the corresponding number--><meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/><body> <a href= ' # ' > First link </a> </br> <a href= ' # ' > Second link < /a> </br> <a href= ' # ' > Third link </a> </br> <a href= ' # ' > Fourth link </a> </br& gt; <script type= "Text/javascript" > var lis = document.links; for (var i = 0, length = lis.length; i < length; i++) { (function (i) { Lis[i].onclick = function () { alert (i + 1);} ) (i); } </script></body>
9, this
function Jsclass () { this.m_text = ' division element '; This.m_element = document.createelement (' div '); This.m_Element.innerHTML = This.m_text; This.m_Element.addEventListener (' click ', This.func); This.m_Element.onclick = This.func;} JSClass.prototype.Render = function () { document.body.appendChild (this.m_element);} JSClass.prototype.func = function () { alert (this.m_text);}; var JC = new Jsclass (); JC. Render (); Add Divjc.func (); Output Division Element//click added DIV elements Division element will output underfined, why?
Answer: Division element undefined
The first output is very good understanding, the second time look carefully, this actually has pointed to the this.m_element, because is the this.m_element call AddEventListener function, so the internal this all points to it. You can try adding a line of code this.m_Element.m_Text = ' Hello World ' to alert you to the Hello world.
10. Split
Write a JavaScript function parsequerystring, which is used to parse URL parameters into an object, such as: var url = "Http://witmax.cn/index.php?key0=0&key1=1 &key2=2″
function parsequerystring (URL) { var obj = {}; var a = Url.split ('? '); if (a.length = = = 1) return obj; var B = a[1].split (' & '); for (var i = 0, length = b.length; i < length; i++) { var c = b[i].split (' = '); Obj[c[0]] = c[1]; } return obj;} var url = ' http://witmax.cn/index.php?key0=0&key1=1&key2=2 '; var obj = parsequerystring (URL); Console.log ( Obj.key0, Obj.key1, Obj.key2);
Do you have these 10 JavaScript pen questions?