Five typical JavaScript-side questions

Source: Internet
Author: User

Read the original

In the IT world, a lot of JavaScript developers are needed. If you are capable of this role, you will have many opportunities to change jobs and raise salaries. But before you get accepted into the company, you need to demonstrate your technical prowess in order to pass the interview session. In this article, I'll show you the 5 typical questions and workarounds that the front-end interview will be asked about, and these 5 questions are often used to test the interviewer's JavaScript skills. They are very interesting!

Issue 1: Range (scope)

Consider the following code:

What does the console print?

Answer

The above code will print out 5 .

The pitfall of this problem is that in the immediate execution of the function expression (iife), there are two names, but the variables are var declared by keywords. This means that it a is a local variable of the function. In contrast, b it is under the global scope.

Another pitfall of this problem is that he does not use "strict mode" () in the function ‘use strict‘; . If strict mode is turned on, then the code will report an uncaught reference error (uncaught referenceerror): B is not defined. Remember, strict mode requires that you explicitly reference the variable when you need to use a global variable. So, you need to write like this:

Issue 2: Create a native (native) method

Stringdefine a function on the object repeatify . This function takes an integer argument to clarify that the string needs to be repeated several times. This function requires the string to repeat the specified number of times. As an example:


Should print out hellohellohello .

Answer

One possible implementation is as follows:


This question tests the developer's mastery of the inheritance in JavaScript, as well as prototype this attribute. This also verifies that the developer has the ability to extend the capabilities of the native data type (although this should not be done).

Another point of this question is to verify that you are aware of and know how to avoid overwriting existing functions. This can be done by judging whether the function exists before the custom function.


This technique is useful when you need to implement backward-compatible functions for your old browser.

Issue 3: Variable elevation (hoisting)

What is the result of executing the following code? Why?


Answer

The result of the execution of this code is undefined and 2 .

The reason for this result is that variables and functions have been promoted (hoisted). Therefore, a when it is printed, it already exists in the scope of the function (that is, it has been declared), but its value remains undefined . In other words, the above code is equivalent to the following code.


Question 4: this  How it works in JavaScript

What is the result of the following code? Please explain your answer.


Answer

The above code prints out the Aurelio De Rosa  and John Doe . The reason is that in JavaScript, the context of a function, the this object referenced by a keyword, depends on how the function is called, not on how the function is defined.

In the first console.log()  call, getFullname()  it is called as a obj.prop  function. Therefore, the context here points to the latter and the function returns this the properties of the object fullname  . Conversely, when getFullname() assigned the test value of a variable, that context points to the global object ( window ). This is because the test property is implicitly set to the global object. Therefore, the value of window  the property returned by the function call fullname  , in this code, is set by the first row assignment statement.

question 5: call() and apply()

Fix the last problem and let the final console.log()  print out Aurelio De Rosa .

Answer

To solve this problem, you can pass call()  apply()  the context of the call to the function or the coercion function. If you do not know call()  and apply()  the difference between, I recommend reading the article " between Function.call and function.apply there and difference?". I'll use it in the following code, call() but here apply()  you can get the same result:


Conclusion

In this article, we discuss the 5 typical questions that JavaScript interviewers often ask about in an interview. Real questions may vary in actual interviews, but the concepts involved and the topics covered are often very similar. I hope you think the tests ahead are interesting. If you don't know some of these answers, don't worry: as you continue to learn and experience, you can solve those problems.

If you've been asked other interesting questions in the interview, don't hesitate to share them with us right away. This will help a lot of developers.

Five typical JavaScript-side questions

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.