First of all, the difference between null and undefined:
Performs typeof on declared but uninitialized and undeclared variables, and returns "undefined".
Null represents an empty object pointer, and the typeof operation returns "Object".
It is generally not explicit to set the value of the variable to undefined, but null instead, for the variable that will save the object, make it clear that the variable holds the null value.
1 2 3 4 5 6 7 |
var bj; Alert (BJ); "undefined" BJ = null; Alert (typeof BJ); "Object" alert (BJ = = null); True BJ = {}; Alert (BJ = = null); False |
The following two functions are Deng Shi brother to me, thank you ah.
1 2 3 4 5 6 7 8 9 10 11-12 |
* * Detects if an object is an empty object (does not contain any readable properties). The * method detects both the properties of the object itself and the attributes inherited from the stereotype (and therefore does not make hasownproperty). */function IsEmpty (obj) {for (var name in obj) {return false;} return true;}; |
The empty object, in the end, is {} or null. I wrote a test case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24-25 |
var a = {}; A.name = ' Realwall '; Console.log (IsEmpty (a)); False Console.log (IsEmpty ({})); True Console.log (IsEmpty (null)); True//Note No syntax error when parameter is null Oh, that is, although you cannot add a property to a NULL Null pointer object, you can use the for in statement? * * Detects if an object is an empty object (does not contain any readable properties). The * method detects only the properties of the object itself and does not detect inherited properties from the stereotype. */function Isownempty (obj) {for (var name in obj) {if (Obj.hasownproperty (name)) {return false;}} return true; |
{} differs from null:
This thing is very important.
|
var a = {}; var b = null; a.name = ' Realwall '; B.name = ' Jim '; This will be an error, B is a null pointer object, you cannot add attributes directly like normal objects. b = A; B.name = ' Jim '; A and B point to the same object at this point. A.name, b.name are ' jam ' |