Hacker News new | past | comments | ask | show | jobs | submit login
Ninja Object Properties in JavaScript (patrick-wied.at)
26 points by mindhunter on June 5, 2012 | hide | past | favorite | 4 comments



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:

package blah1.blah2; class Blah3 { private static final String BLAH_KEY = "blah1.blah2.Blah3.BLAH_KEY"

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.

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.


hey, I'm the author of the article. I didn't write that using dots in the property name is a bad practice, but rather that other parts of the code let me assume that they didn't know a lot about js


Remember, \u200B is also valid in property names, making it immune to

for(i in x){ console.log(i, "_"); }

as well. That said, this is the worst idea.


It gets even more weird when using Unicode imperceptible characters

    x = {}
    x[String.fromCharCode(1)] = "1"
    x[String.fromCharCode(2)] = "2"
    x[String.fromCharCode(3)] = "3"
And obligatory reference to namespace function (to easily create namespaced modules): http://www.codeproject.com/Articles/19030/Namespaces-in-Java...




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: