0. Greeting
Turn over their own blog, the last one is June 26, said the update? Review just past this July, all day is the company's induction training plus their own small papers, every day to fight to 1 o'clock in the morning more, this is to hang the rhythm Ah! However, no matter how to say, their own time management is still out of the question, it must be well planned later, not many said, to the point!
Why do you write this article? Because after eating breakfast, digestion, a few lines of code, and then found the whole hanging ...
The approximate code is as follows:
function Getbyclass (clsname, parent) { if(parent== "undefined") { var oparent= document; } Else { var oparent=document.getElementById (parent); } // The following code is omitted }
The above method when judging the second optional parameter, if there is no parameter, I have to deal with it, and then the program is stuck here. Finally replaced by the undefined keyword can be, here will involve undefined and some of the use of NULL, specific different places, please continue to look down.
The concept of NULL, undefined, and Nan
1. In JS, I have always felt that the difference between undefined and null is not very small, usually use the time will not be too concerned. Generally speaking:
Undefined generally means "nonexistent" at all, and null means defined, but the value is "empty".
However, we find that there is no undefined in compiled languages such as Java. What is this for? Java is a static type language, all variables are initialized with a strict type declaration, if you define a undefined variable, I am afraid that the compilation will be a click Erase, but in Java is allowed to define NULL to represent a null value. JavaScript is a dynamic interpretive language, and for non-existent types, judgment is only made when the interpretation is run.
2. Under what circumstances will it be null?
// (1) When assigning a value to a variable or object, assign null, it is not appropriate to assign a value of undefined var parm=null; // (2) The DOM returns NULL when the element is not acquired var dom = document.getElementById (' Domid '); // NULL // (3) The end point of the other prototype chain, which also returns null Object.getprototypeof (Object.prototype)//null
3. What is the use of undefined?
There are several main cases:
(1) Declaring a variable, but not assigned a value
(2) function has no return value and returns undefined after execution
(3) optional parameter in function, return undefined when no parameter is passed
(3) An attribute that does not exist or is not assigned to a value in an object
Look at the following example code:
var A;console.log (a) // undefined var foo=function() {}console.log (foo ()); // undefined function Geti (i) { console.log (i);} Geti (); // undefined var o={};
Console.log (o.name); // undefined
4. See below for undefined and null conversions to numeric values, typeof above
Number (undefined); // NaN Number (null); // 0 typeof (undefined) // "undefined" typeof (null) // "Object"
As can be seen from the above, undefined is converted to a value of Nan, and Null can be converted to 0, which is the difference between the two, and after TypeOf detection, they return "undefined", "Object"
"Note" typeof returns a string with six possibilities: "Number", "string", "Boolean", "Object", "function", "undefined", and this follow-up can serve the detection of both.
5.NaN
In general, when an operation cannot return the correct value, it returns a value of "NaN". The Nan value is "not a number", so it is impossible to handle when comparing the size, and it is more important to note that Nan itself is not equal to Nan. We can use JavaScript's built-in function isNaN () to detect whether a value is numeric.
Second, how to judge undefined, null and value
1. Judge undefined:
var parm=undefined; if (typeof(parm) = = "undefined") { alert ("I am undefined"); }
Note that typeof is the returned string and needs to turn undefined into a string comparison
2. Determine null:
var NULL ; if typeof (parm)! = "undefined" && parm!=0) { alert ("I am null"); }
It is more troublesome to judge only null. First, it is converted to the bool value judgment, where undefined, null and numeric values can appear, and then the TypeOf is used to filter out the undefined;
3. Judge Nan:
var parm= 0/0; if (IsNaN (parm)) { alert ("I am NaN"); }
The "note" IsNaN () function can be used to detect anomalies that may occur after some operations, such as 0 for divisor and so on, as well as to detect if the results of parsefloat () and parseint () processing are numeric, thus reducing the likelihood of exceptions that may be thrown by the program
4. Also judge undefined and null:
var parm= undefined; // var parm= null; if (parm== undefined) { alert ("I am null or undefined");
Since the general null==undefined return is true, we can use the above method to detect.
5. Determine undefined, null, and value at the same time:
var NULL ; // var parm= undefined; // var parm= 0; if (! parm) { alert ("I am null or undefined or 0"); }
In general, we JS judgment, do not need to strictly detect, direct!parm on it can be.
Iii. Summary
In general, this is a matter of detail, but the more you grasp the details, it is not easy to make mistakes in the actual code process. I am a front-end rookie, I hope this article can inspire you, the text has the wrong place to understand, I hope you correct. If your friends and soft sister feel that the article is helpful to you, your message and recommendation will be a great encouragement to me! Good afternoon, ladies and gentlemen.