Essays, documenting the questions of a polygon in JavaScript

Source: Internet
Author: User

I do not know my JS level how, but today a bit hit, see a lot of problems have pits. After reading some interview questions, I recorded the pits I met today.

Issue 1: Scope

Consider the following code, what is the output?

(functionvar a = b = 5;}) (); Console.log (b);

The answer is 5.

The problem I know, the trap is that var a = b = 5, if it is a declaration, it should be Var, a, B = 5, so the value of a, B is 5, because a, B is in the scope of the function, so the external output is not out, it will show B = undefined. And here, because of the JS characteristics, so this is equivalent to

var A,
b = 5

The B = 5 here is an implicit variable !!! So the equivalent of window.b = 5, so the external is accessible.

Issue 2: Create a "built-in" method

Defines a repeatify method for a string object. The method receives an integer argument, as the number of times the string repeats, and finally returns a string that repeats the specified number of times. For example:

Console.log (' Hello '. repeatify (3));

The output should be:

Hellohellohello.

See this problem, I was thinking that the purpose of this problem is to solve the Hello string, and then to copy 3 of this string, then this particular to I just saw a function, a will be pasted.

   function (times) {                var str = '                ;  for var i = 0; I < times; i++ ) {                    this;                }                 return str;            };            ' Hello '. Repeatify (3));

What should be examined here is the understanding of Proptotype, which is the archetype. Explain, deepen the impact!! String.prototype.repeatify is equivalent to adding a custom Repeatify method to the prototype of string, which functions as a method,

First, a str, or an empty string, is defined to accept the arguments. Repeatify (3) is to copy 3 copies of the output, then traverse it, there are several times, add several, here the parameters 3 times, then for the Loop 3 times, let Str plus this, here is the reference to the self, is str. Equals Str=str+str+str, the last return, the STR as the value output, remember today to see a sentence, function is to find the value!!!!! This sentence is very important, is to use the value to do other things!!

There is a key point, that is | | Judge!!! If this method is available, skip it and use the original method.

Issue 3: Declaration in advance

What is the result of the following code? Why?

function Test () {       console.log (a);   // undefined       Console.log (foo ());  // 2       var a = 1;        function foo () {           return 2;       }   }   Test ();

I have knocked out the answer! It turns out I'm not bad, here I need to explain why the first A is undefined? Very simple, here is the declaration of Ascension, the equivalent of Var a advance to console.log (a); Go up, here a actually does not have the assignment value, therefore is undefined, so why can the function return 2?! Because Console.log (foo ()) is a function call, it is possible to write anywhere in the function body.

The This in question 4:javascript

What is the result of the following code and explanation.

 var  fullname = ' John Doe ' ;  var  obj = {fullname:  ' Colin Ihrig ' 

The answer I also write out, also is relatively simple, mainly is to this the inspection, heard the interview time is must test this! So, I'd like to see more examples.

One key point to remember is that the function context that the keyword this refers to in JavaScript depends on how the function is called, not how it is defined.

So it's simple, the first this is called the FullName in prop, and the second test is defined in the Global (window), so it is called in the global, then it is called in the globally defined FullName. Still not difficult ha, hope I later have a disciple, can see these notes, is also a record of my growth, I want to become a great engineer, and not a dabbler or only know to write code of yards farm! I want to understand the principle, this is the meaning of my life, pull away. Next!

Question 5:call () and apply ()

Fix the previous issue and let the last Console.log () print out Aurelio De Rosa.

Visit Call and apply ... I am a little weak, because I really do not understand ah, I need to take a good look.

This problem can be cast by using the call () or the Apply () method to force the context.

Console.log (Test.call (Obj.prop));

Over here.. I'll cover my face and read a book.

There's one more question!! MET today!! Oh mom, I did wrong, thinking for a long time, and finally found the answer on the internet for help

var a = {N:1};   var b = A; a.x = a = {N:2};  alert (a.x); // - undefined   alert (b.x); // -- Object {N:2}

Why is the a.x here undefined? I was thinking for a long time not to understand ... Now I see, a.x = a = {N:2}; because the. operator takes precedence over =, this line of code is actually parsed as:

1, a.x (creates an X object in a, the value is null), and because B = A, there is also an x in B is null

2, a = {N:2}; What does that mean? That is, because the A object has n, so directly modify its value (oops, it reminds me that the creation of the string, the array is not modifiable!!) And the object: If you want to change, no human rights Ah! )

3, because there is x in B, then add {N:2} to B.

The result of the assignment is:

= = {N:1, x: {N:2}}  

And a bit of a detour! Still, I got it! La La la ~

After checking back--. (dot), () (parentheses), [] (brackets) These three are the highest in JS!

Essays, documenting the questions of a polygon in JavaScript

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.