Consider the JavaScript code:
var y = "12";
function f() {
var y = "6";
alert(this.y);
function g() { alert(y); }
g();
}
f();If M is the number of alert dialog boxes generated by this code and D1, D2, …, DM are the contents displayed in each, then:
Pick an option to check yourself, or open the answer below.
Show answer & explanation
Correct answer: D. M=2; D1 displays ”12”; D2 displays ”6”
Explanation
f() is called as a plain function, so this is the global object and this.y is "12". g() reads the closest y in scope, f's local "6". Two alerts: "12" then "6".
More Programming Languages and Computer Graphics questions
- What is the output of the following Java program? class simple { public static void main(String[] args) { simple obj = new simple(); obj.st…
- What is the output of the following C program? (Assume little-endian representation, with the least significant byte at the lowest address,…
- Given below are three implementations of swap() in C++: (a) void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } called as swap…
- In Java, which of the following statements is/are True ? S1 : The ‘final’ keyword applied to a class definition prevents the class from bei…
- Which of the following statements is/are True ? P : C programming language has a weak type system with static types. Q : Java programming l…
- A graphic display system has a frame buffer that is 640 pixels wide, 480 pixels high and 1 bit of color depth. If the access time for each…