TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The void of undefined in JavaScript

51 点作者 Nassfyr将近 12 年前

11 条评论

ars将近 12 年前
The correct solution for this problem of undefined is to do nothing!<p>If someone redefined undefined and it causes a problem - too bad! Some problems are just too stupid to worry about.
评论 #6032462 未加载
评论 #6033426 未加载
drostie将近 12 年前
&gt; <i>Let&#x27;s say someone is using your library within that function and you reference undefined. You get the string &quot;oops&quot;. Oops indeed.</i><p>That is incorrect. It might be correct if the word &#x27;library&#x27; was replaced by &#x27;code snippet&#x27; to indicate a copy-and-paste-of-your-code issue. But if you&#x27;ve created a <i>library</i> then your functions are over in some other file, where `typeof undefined === &quot;undefined&quot;`.<p>The proper attitude here is the same as the Python attitude towards not having `private` attributes: &quot;If some other programmers want to do something crazy with my code, that&#x27;s their prerogative. If it blows up in their faces, that&#x27;s their problem.&quot;
评论 #6033117 未加载
评论 #6032838 未加载
marijn将近 12 年前
I find the cult of &#x27;<i>oh my god you can redefine </i><i>undefined</i><i>&#x27;</i> hilarious. Yes, you can. You can also redefine <i></i>Array<i></i>, <i></i>Object<i></i>, and so on. If you&#x27;re writing a script to intentionally disrupt a system, <i></i>for(;;);<i></i> will also do. This doesn&#x27;t happen in sane environments, so it really isn&#x27;t a problem.<p>The Crockford quote (characteristically dogmatic, to the effect of &#x27;<i>void means something different in JS than in Java, so AVOID VOID!</i>&#x27;) is also a kicker.
lubomir将近 12 年前
&gt; […] if you throw your scripts out there on the web you&#x27;ve got to expect that somewhere, at some time someone is going to do it […]<p>And then it will be that persons&#x27; problem. Their code would be wrong, not mine. By not making sure my code works when undefined is broken, I would be helping them to realize they have a possible bug in their codebase and that they need to fix it.
评论 #6032156 未加载
benaiah将近 12 年前
Isn&#x27;t this whole issue just a minor version of the problem Ruby has with monkeypatching? The fact that a Ruby guy can define `method_missing` to allow for bare strings shows how Ruby is cool (though you should never do that), but the fact that you can redefine `undefined` in JS shows how JS is stupid (despite the fact that you should never do that). I don&#x27;t understand the dichotomy.
评论 #6032425 未加载
mkohlmyr将近 12 年前
I tend to read these sorts of articles as &quot;don&#x27;t do stupid things&quot;. Who in their right mind would name an argument undefined? (or use a library by someone who would do so) The operative sentence in the article to me is &quot;can be avoid if you understand how it works&quot;. On an unrelated note I&#x27;m not sure if that&#x27;s a typo or a pun.
ChrisAntaki将近 12 年前
Has anyone ever seen &quot;undefined&quot; be redefined, on one of their professional projects?
评论 #6032110 未加载
评论 #6032102 未加载
评论 #6032571 未加载
评论 #6032130 未加载
wldlyinaccurate将近 12 年前
jQuery does it right:<p><pre><code> (function( window, undefined ) { &#x2F;&#x2F; ... })( window );</code></pre>
scrabble将近 12 年前
There are a lot of potential pitfalls in coding JavaScript. Redefining undefined is only one of many.<p>One of the nice things about JavaScript is that it gives you the ability to accomplish things many different ways, but if someone uses that to shoot themselves in the foot then that is something they need to correct.
lelf将近 12 年前
&gt; <i>If you have done more than one day&#x27;s programming in any language you will realise that this is an important building block for programmers.</i><p>I guess my <i>any</i> languages are different. Never realized it&#x27;s importain in haskell (except it is ⊥).
评论 #6037162 未加载
aphelion将近 12 年前
The worst thing about undefined is not its mutability but that it exists in the first place. Trying to retrieve a non-existent attribute should throw an error by default, not fail silently.