1:scope Scope of Action
Copy Code code as follows:
(function () {
var a = b = 5;
})();
Console.log (b);
What will be printed on the console?
Reply
The code above will print 5.
The trick to this problem is that there are two variable declarations here, but a is declared using the keyword var. Represents that it is a local variable of a function. In contrast, B becomes a global variable.
Another trick of this problem is that it does not use strict mode (' 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 an explicit designation in order to implement a global variable declaration. For example, you should write:
Copy Code code as follows:
(function () {
' Use strict ';
var a = window.b = 5;
})();
Console.log (b);
2: Create the "native" (native) method
Defines a repeatify function for a string object. When you pass in an integer n, it returns the result of the repeated N-second string. For example:
Copy Code code as follows:
Console.log (' Hello '. repeatify (3));
Hellohellohello should be printed.
Reply
A possible implementation is shown below:
Copy Code code 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 problem is testing the developer's knowledge about JavaScript inheritance and prototype. This also validates the developer's knowledge of whether 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 functionality that might have been defined. The definition of the feature does not exist until you test it:
Copy Code code as follows:
String.prototype.repeatify = String.prototype.repeatify | | function (times) {/* code here/};
This technique is especially useful when you are asked to do JavaScript function compatibility.
3: Announcement Promotion (hoisting)
Execute this code and output what results.
Copy Code code as follows:
function Test () {
Console.log (a);
Console.log (foo ());
var a = 1;
function foo () {
return 2;
}
}
10:test ();
Reply
The result of this code is undefined and 2.
The reason is that the declarations of variables and functions have been advanced (moved to the top of the function), but the variables do not assign any values. Therefore, when you print a variable, it exists in the function (it is declared), but it is still undefined. In other words, the above code equates to the following:
Copy Code code as follows:
function Test () {
var A;
function foo () {
return 2;
}
Console.log (a);
Console.log (foo ());
A = 1;
}
Test ();
How 4:this works in JavaScript
What does the following code output? Give your answer.
Copy Code code as follows:
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 ());
Reply
The answer is Aurelio De Rosa and John Doe. The reason is that in a function, this behavior depends on how the JavaScript function is invoked and how it is defined, not just how it is defined.
In the first console.log () call, Getfullname () is invoked as a function of the Obj.prop object. So, the context refers to the latter, and the function returns the FullName of the object. In contrast, 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.
5:call () and apply ()
Now let you solve the previous problem and make the final Console.log () print Aurelio De Rosa.
Reply
The problem can be changed by forcing the call () or apply () to change the function context. I will use Call () below, but in this case, apply () will output the same result:
Copy Code code as follows:
Console.log (Test.call (Obj.prop));