我已经写了很长时间的 JavaScript,而且我从来没有理由使用 null
。似乎 undefined
总是更可取并且以编程方式服务于相同的目的。使用 null
而不是 undefined
的一些实际原因是什么?
最佳答案
我真的没有答案,但根据 Nicholas C. Zakas,他的书第 30 页 "Professional JavaScript for Web Developers" :
When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to
null
as opposed to anything else. That way, you can explicitly check for the valuenull
to determine if the variable has been filled with an object reference at a later time
关于javascript - 在 JavaScript 中使用 null 而不是 undefined 的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6604749/