1

I'm currectly reading the book JavaScript: The Good Parts by Douglas Crockford.

The opening of the Chapter 3 Objects immediately confuses me with a (possible) contradiction. I re-read the two paragraphs at least a dozen of times and am still not sure which part is correct, and which is wrong.

I will definitely try things out in the browser console, but I don't want to miss an important detail in case there's no error nor contradiction in the book. Could anyone clarify me this?

An object is a container of properties, where a property has a name and a value. ... A property value can be any JavaScript value except for undefined.

... There is no constraint on the names of new properties or on the values of properties.

3

I don't have my copy of the book on hand, but undefined is valid for both property names and values. It's simple enough to demo in a browser console.

var obj = {
    foo: undefined
};
obj.bar = undefined;
obj[undefined] = undefined;

obj.hasOwnProperty("foo");  // === true
obj.propertyIsEnumerable("bar");  // === true
obj.propertyIsEnumerable(undefined);  // === true

The O'Reilly website maintains lists of confirmed and unconfirmed errata. If you feel the book is in error, you can submit your own errata.

1

What he is really saying, without coming right out and saying it, is JavaScript objects are just key-value pairs.

In Java, it would be a HashTable<String, Object>

In C# it would be a Dictionary<string, object>.

JavaScript objects are just a bag of keys as strings, and values associated with each key being any type that exists in JavaScript.

var x = {
    foo: false
};

You can access the foo property value be either means:

x.foo
x["foo"]

Furthermore, you can use any valid string characters for a property name if you use the array notation:

x["@%$!$5@^^"] = 8;

This array notation mimics the syntax of accessing or setting values on a Dictionary object in C#:

Dictionary<string, object> x = new Dictionary<string, object>();

x["foo"] = false;
x["@%$!$5@^^"] = 8;

Hence the association that Douglas Crockford is making about JavaScript objects being "a container of properties, where a property has a name and a value".

The last thing he might not have written (or you haven't read it yet) is property names in JavaScript are always converted to strings:

x[undefined] = 4;
x["undefined"] = 9;

Screenshot from Firefox's debugging tools:

Screenshot of Firefox debugging tools illustrating undefined property

  • While all you're saying is understandable and correct, the focus of my question is on highlighted parts of the text. One sentence is saying the values cannot be undefined (which is a constraint), while the other sentence says there are no constraints on values. This is what's confusing. – Igor Soloydenko Oct 6 '17 at 17:31
  • 1
    @IgorSoloydenko: He is just not correct, in this case. Maybe a typo that didn't get caught during the editing process? Could also stem for x.foo === undefined is a common test to see if a property has been defined and x.foo = undefined kind of invalidates that test? – Greg Burghardt Oct 6 '17 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.