Problem 1:scope range of action
Consider the following code:
(function () {
var a = b = 5;
})();
Console.log (b);
What will be printed on the console?
Answer
The above code will print 5.
The trick to this question is that there are two variable declarations, but a is declared with the keyword var. Represents a local variable of a function. In contrast, B becomes a global variable.
Another trick of this question is that it does not use strict mode (' using strict ';). If strict mode is enabled, the code throws a Referenceerror error: B is undefined (b is not defined). Keep in mind that strict mode requires explicit designation in order to implement a global variable declaration. For example, you should write:
(function () {
' Use strict ';
var a = window.b = 5;
})();
Console.log (b);
Issue 2: Create a "native" (native) method
Defines a repeatify function for a string object. When an integer n is passed in, it returns the result of repeating the n-th string. For example:
Console.log (' Hello '. repeatify (3));
The Hellohellohello should be printed.
Answer
One possible implementation is as follows:
String.prototype.repeatify = String.prototype.repeatify | | function (times) {
var str = ';
for (var i = 0; I < times; i++) {
str + = this;
}
return str;
};
Now the question is tested by developers about JavaScript inheritance and prototype knowledge points. This also verifies whether the developer knows how to extend the built-in objects (although this should not be done).
Another important point here is that you need to know how to not overwrite features that might already be defined. Do not exist until you test the function definition:
String.prototype.repeatify = String.prototype.repeatify | | function (times) {/* code here */};
This technique is especially useful when you are asked to do JavaScript functions that are compatible.
Issue 3: Claim elevation (hoisting)
Execute this code and output what results.
function Test () {
Console.log (a);
Console.log (foo ());
var a = 1;
function foo () {
return 2;
}
}
Test ();
Answer
The result of this code is undefined and 2.
The reason is that the declarations of variables and functions are advanced (moved to the top of the function), but the variables do not have any values assigned. Therefore, when the variable is printed, it exists in the function (it is declared), but it is still undefined. In other words, the above code is equivalent to the following:
function Test () {
var A;
function foo () {
return 2;
}
Console.log (a);
Console.log (foo ());
A = 1;
}
Test ();
Question 4:this How it works in JavaScript
What result does the following code output? Give 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 ());
Answer
The answer is Aurelio De Rosa and John Doe. The reason is that in a function, the behavior of this depends on how the JavaScript function is called and how it is defined, not just how it is defined.
In the first console.log () call, Getfullname () is called as a function of the Obj.prop object. So, the context refers to the latter, and the function returns the FullName of the object. Conversely, when Getfullname () is assigned to the test variable, the context refers to the Global Object (window). This is because test is a property that is implicitly set to the global object. For this reason, the function returns the FullName of the window, that is, the value defined in the first row.
Question 5:call () and apply ()
Now let you solve the previous problem and make the final Console.log () print Aurelio De Rosa.
Answer
The problem can change the function context by forcing the use of call () or apply (). I will use Call () below, but in this case, apply () will output the same result:
Console.log (Test.call (Obj.prop));
Conclusion
5 Classic JavaScript Interview questions