TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ninja Object Properties in JavaScript

26 pointsby mindhunteralmost 13 years ago

3 comments

lnanek2almost 13 years ago
It isn't unknown, or even bad practice, to have string values with dots in them when needed in Java to avoid collisions. For example, the package names of Android apps use that format, even though the dots in them mean nothing. Often a constant in a class will have the exact same String value as it's location:<p>package blah1.blah2; class Blah3 { private static final String BLAH_KEY = "blah1.blah2.Blah3.BLAH_KEY"<p>Which helps a lot when you see the value somewhere else as well. Often if you are told to put some String keyed values into a general purpose message container going to another program, the string keys you use again use values to look like that to avoid collision, as recommended by the Android documentation itself. Similarly used for persisting application values to a single XML file. Then of course we also have technologies like GWT, where programmers write in Java, and it is compiled to JavaScript. I could easily see that generating property names with packages in them.<p>So anyway, you see dots for sub-objects, but you also see dots simply as part of a way to avoid name collisions, as in Java package naming and sharing String key namespace best practices. Just because it isn't used for sub-objects or method calls or properties doesn't mean it is wrong or bad.
评论 #4068957 未加载
KyleHillalmost 13 years ago
Remember, \u200B is also valid in property names, making it immune to<p>for(i in x){ console.log(i, "_"); }<p>as well. That said, this is the worst idea.
jQueryIsAwesomealmost 13 years ago
It gets even more weird when using Unicode imperceptible characters<p><pre><code> x = {} x[String.fromCharCode(1)] = "1" x[String.fromCharCode(2)] = "2" x[String.fromCharCode(3)] = "3" </code></pre> And obligatory reference to namespace function (to easily create namespaced modules): <a href="http://www.codeproject.com/Articles/19030/Namespaces-in-JavaScript" rel="nofollow">http://www.codeproject.com/Articles/19030/Namespaces-in-Java...</a>