JavaScript Questions & Answers - 2
These questions & answers are based on the material from the book Eloquent JavaScript by Marijn Haverbeke, which covers ECMAScript 5 and from the most voted up questions on Stack Overflow.
Q. Why do Google & some other popular websites prepend while(1); to their JSON responses?
A. It prevents JSON hijacking. Long answer
Facebook prepends for (;;); before JSON content. for (;;); is a way to intentionally create a loop that doesn't terminate on its own.
Q. What does “use strict” do in JavaScript?
A. ECMAScript 5 Strict mode helps out in a couple ways:
~ It catches some common coding bloopers, throwing exceptions.
~ It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
~ It disables features that are confusing or poorly thought out.
Normally, when you forget to put var in front of your variable, as with counter in the example, JavaScript quietly creates a global variable and uses that. In strict mode, however, an error is reported instead.
If you put "use strict"; in your code, and run it in a browser that doesn't support it, the statement has no effect. If you run it in a later browser, the browser will check for more errors.
You can apply "strict mode" to the whole file or you can use it only for a specific function
It's supported by all major browsers.
Q. When building a link that has the sole purpose to run JavaScript code, which is better - using “#” or “javascript:void(0)”?
A. Neither. Long answer
Q. Comments in JSON are not allowed. True or False
A. True
Q. What problem does JSONP solve?
A. JSONP or JSON with Padding, is really a simply trick to overcome XMLHttpRequest same domain policy.
Q. What does the debugger statement do?
A. A debugger statement (consisting of simply that keyword) can be used to set a breakpoint in your program. If the Developer tools of your browser are active, the program will pause whenever it reaches that statement, and you will be able to inspect its state.
Q. JavaScript doesn’t provide direct support for selectively catching exceptions: either you catch them all or you don’t catch any. True or False
A. True
Q. What does this statement do:
alert($("*:focus").attr("id"));
A: Alerts id of currently focused element
Q. Other than by using eval, how can you interpret data as code?
A: A better way of interpreting data as code is to use the Function constructor. This takes two arguments: a string containing a comma-separated list of argument names and a string containing the function’s body.
var plusOne = new Function ("n" , "return n + 1;") ;
console.log(plusOne(4)) ;
// 5
Q. What is the difference between the keypress and keydown events?
A: "keypress", which is fired right after "keydown" (and repeated along with "keydown" when the key is held) but only for keys that produce character input.
Q. Why do Google & some other popular websites prepend while(1); to their JSON responses?
A. It prevents JSON hijacking. Long answer
Facebook prepends for (;;); before JSON content. for (;;); is a way to intentionally create a loop that doesn't terminate on its own.
Q. What does “use strict” do in JavaScript?
A. ECMAScript 5 Strict mode helps out in a couple ways:
~ It catches some common coding bloopers, throwing exceptions.
~ It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
~ It disables features that are confusing or poorly thought out.
Normally, when you forget to put var in front of your variable, as with counter in the example, JavaScript quietly creates a global variable and uses that. In strict mode, however, an error is reported instead.
If you put "use strict"; in your code, and run it in a browser that doesn't support it, the statement has no effect. If you run it in a later browser, the browser will check for more errors.
You can apply "strict mode" to the whole file or you can use it only for a specific function
It's supported by all major browsers.
Q. When building a link that has the sole purpose to run JavaScript code, which is better - using “#” or “javascript:void(0)”?
A. Neither. Long answer
Q. Comments in JSON are not allowed. True or False
A. True
Q. What problem does JSONP solve?
A. JSONP or JSON with Padding, is really a simply trick to overcome XMLHttpRequest same domain policy.
Q. What does the debugger statement do?
A. A debugger statement (consisting of simply that keyword) can be used to set a breakpoint in your program. If the Developer tools of your browser are active, the program will pause whenever it reaches that statement, and you will be able to inspect its state.
Q. JavaScript doesn’t provide direct support for selectively catching exceptions: either you catch them all or you don’t catch any. True or False
A. True
Q. What does this statement do:
alert($("*:focus").attr("id"));
A: Alerts id of currently focused element
Q. Other than by using eval, how can you interpret data as code?
A: A better way of interpreting data as code is to use the Function constructor. This takes two arguments: a string containing a comma-separated list of argument names and a string containing the function’s body.
var plusOne = new Function ("n" , "return n + 1;") ;
console.log(plusOne(4)) ;
// 5
Q. What is the difference between the keypress and keydown events?
A: "keypress", which is fired right after "keydown" (and repeated along with "keydown" when the key is held) but only for keys that produce character input.
Comments
Post a Comment