博文

目前显示的是 十二月, 2013的博文

What is the difference between null and undefined in JavaScript?

http://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript There is one difference between undefined And null in javascript :: undefined is a data type and null is a value defined empty variable is null of datatype undefined Both of them are representing a value of a variable with no value AND null doesn't represent a string that has no value Like var a = '' ; console . log ( typeof a ); // string var a = '' ; console . log ( a == null ); //false console . log ( a == undefined ); // false Now if var a ; console . log ( a == null ); //true console . log ( a == undefined ); //true BUT var a ; console . log ( a === null ); //false console . log ( a === undefined ); // true SO each one has it own way to use undefined use it to compare the variable data type null use it to empty a value of a variable var a = 'javascript' ; a = null ...

JavaScript quiz - Scoping related

http://www.nczonline.net/blog/2010/01/26/answering-baranovskiys-javascript-quiz/ Answering Baranovskiy’s JavaScript quiz Posted at January 26, 2010 09:00 am by Nicholas C. Zakas Tags: Hoisting , JavaScript Last week, I tweeted about a JavaScript quiz I came across on Dmitry Baranovskiy’s blog entitled, So you think you know JavaScript? As with other quizzes of this type, there is only one question to answer for five different pieces of example code: what is the result? The example code tests some of the quirkier attributes of JavaScript engine behavior. I’ve seen similar quizzes in the past, sometimes by people saying that they use it as a test during job interviews. I think doing so is both disrespectful to the candidate as well as generally useless. You don’t encounter this type of quirk every day, so making that the minimum to get a job is about as useful as asking flight attendant candidate...

JavaScript Scoping

From: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html Do you know what value will be alerted if the following is executed as a JavaScript program? var foo = 1; function bar() { if (!foo) { var foo = 10; } alert(foo); } bar(); If it surprises you that the answer is “10”, then this one will probably really throw you for a loop: var a = 1; function b() { a = 10; return; function a() {} } b(); alert(a); Here, of course, the browser will alert “1”. So what’s going on here? While it might seem strange, dangerous, and confusing, this is actually a powerful and expressive feature of the language. I don’t know if there is a standard name for this specific behavior, but I’ve come to like the term “hoisting”. This article will try to shed some light on this mechanism, but first lets take a necessary detour to understand JavaScript’s scoping. Scoping in JavaScript One of the sources of most confusion for JavaScript beginners is scoping. Actuall...