nijikokun / the-zen-approach

JavaScript Styleguide, can be applied to other things as well.
MIT License
168 stars 16 forks source link

Reserved words on Objects #8

Closed Cleod9 closed 10 years ago

Cleod9 commented 10 years ago

I think you may want to elaborate on the following in your write-up:

"If you use reserved words on objects and attempt to access them through dot notation, your code will not work in older versions of IE."

var example = {
  "private": function () {}
};

// Can and will, break in older browsers.
example.private;

// Will work in older browsers.
// Acceptable, in my opinion only for the three situations I mentioned earlier.
example['private'];

So, I don't believe this is accurate. As far as the JavaScript interpreter is concerned, there are no limitations on object property names as long as they are initialized correctly. In your example, initializing without quotes would be the part to cause potential browsers issues. For example:

var obj1 = { function: function() {} }; //Errors in IE7
var obj2 = { "function": function() {} }; //Works in IE7

Even though "function" is a reserved word, when it's inside of a String it's not interpreted as an Identifier and works just fine. I recommend revising this part of your text to make sure it's accurate. Also it may be helpful if you provide exact version numbers of IE (or other browsers) where you say certain things won't work.

For reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words#Reserved_word_usage

(In any case, great writing!)

nijikokun commented 10 years ago

You are correct, I changed this over to use quotations after writing this and forgot the implications of doing so, thank you for pointing this out.

nijikokun commented 10 years ago

has been updated, closing.