Showing posts with label JavaScript Prototype. Show all posts
Showing posts with label JavaScript Prototype. Show all posts

Tuesday, December 6, 2016

JavaScript hoisting - Q&A

Question:

What’s the result of executing this code and why?










Answer

The reason is that both variables and functions are hoisted (moved at the top of the function) but variables don’t retain any assigned value. So, at the time the variable a is printed, it exists in the function (it’s declared) but it’s still undefined. Stated in other words, the code above is equivalent to the following:



JavaScript Prototype and inheritance - Q&A

Question:

Define a repeatify function on the String object. The function accepts an integer that specifies how many times the string has to be repeated. The function returns the string repeated the number of times specified. For example:





Should print hellohellohello.

Answer


The question tests the knowledge of the developer about inheritance in JavaScript and the prototype property. It also verifies that the developer is able to extend native data type functionalities (although this should not be done).
Another important point here is to demonstrate that you are aware about how to not override possible already defined functions. This is done by testing that the function didn’t exist before defining your own.