10 Typical JavaScript questions (i)

Source: Internet
Author: User
Tags extend
Reproduced from: http://www.css88.com/archives/7052
Issue 1: Scope (scope) (function () {var a = b = 5;})   (); Console.log (b); What the console (console) will print.

The code above will print out 5.
The pitfall of this problem is that there are two assignments in the immediate execution function expression (iife), but where variable a is declared using the keyword var. This means that a is a local variable of this function. In contrast, B is assigned to the global scope (that is, the global variable). Another pitfall of this problem is that the "strict mode" (' Use strict ';) is not used in the function. If the strict mode is turned on, the code will complain "uncaught referenceerror:b is not defined". Keep in mind that if this is the expected behavior, strict mode requires that you explicitly refer to the global scope. So you need to write as follows: (function () {' Use strict '; var a = window.b = 5;})   (); Console.log (b);
Issue 2: Create a native (native) method to define a repeatify function on a String object. This function takes an integer argument to make it clear that the string needs to be repeated several times. This function requires a string to repeat the specified number of times. For example: Console.log (' Hello '. repeatify (3)); Should print out Hellohellohello. A feasible approach is as follows: String.prototype.repeatify = String.prototype.repeatify | |   function (times) {var str = ';   for (var i = 0; I < times; i++) {str + this;} return str; };
This question tests the developer's knowledge of the inheritance and prototyping (prototype) attributes in JavaScript. This also verifies that developers have the ability to extend native data type functionality (although this should not be the way). Another key point here is to see how you can avoid rewriting methods that might have been defined. This can be done by detecting whether the method already exists before defining its own method. This technique is useful when you are asked to extend a JavaScript method.

Question 3: What is the result of a variable elevation (hoisting) that executes the following code? Why.   function test () {Console.log (a); Console.log (foo ()); var a = 1; function foo () {return 2;}} Test ();
The result of this code is undefined and 2. The reason for this result is that the variables and functions are promoted (hoisted) to the top of the function body. Therefore, when the variable A is printed, it exists in the function body (because a is already declared), but it is still undefined.   In other words, the above code is equivalent to the following code: function Test () {var A; function foo () {return 2;} Console.log (a); Console.log (foo ()); A = 1; Test ();
Question 4: In JavaScript, ' This ' is the result of how the following code works. Please explain your answer. var fullname = ' John Doe '; var obj = {fullname: ' Colin ihrig ', prop: {fullname: ' Aurelio De Rosa ', getfullname:function () {return this.fullname;   } } };   Console.log (Obj.prop.getFullname ());   var test = Obj.prop.getFullname; Console.log (Test ());
This code prints the result: Aurelio De Rosa and John Doe. The reason is that the keyword this in JavaScript refers to the function context, depending on how the function is invoked, not how it is defined. In the first Console.log (), Getfullname () is invoked as a function of the Obj.prop object. Therefore, the current context refers to the latter, and the function returns the FullName property of the object. Conversely, when Getfullname () is assigned to the test variable, the current context is the Global object window because test is implicitly a property of the global object. Based on this, the function returns the FullName of the window, which in this case is set for the first line of code.

The problem 5:call () and apply () fix the previous problem and let the last Console.log () print out Aurelio De Rosa. This problem can be enforced by using the call () or the Apply () method to cast the context. If you do not understand these two methods and their differences, I suggest you look at this article function.call and function.apply between and the difference? In the following code, I use Call (), but apply () can produce the same result: Console.log (Test.call (Obj.prop));

Related Article

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.