I am reading a book JavaScript: The Good Parts for increasing my JavaScript knowledge. In Chapter 3 Objects the Author says:
An object is a container of properties, where a property has a name and a value. A property name can be any string, including the empty string. A property value can be any JavaScript value except for undefined.
What does the Author mean by that? As far as I know, we can set values to undefined in an object literal or outside of an object literal using with prototype word. For example Object.prototype.customName = undefined; works well. What am I missing here?
You're right that you can assign the value undefined to a property, and even define a literal with the undefined property; however, the resulting object is very easily confused with the similar object written without that property.
For example, in Node:
> x = {a: 1, b: undefined}
{ a: 1, b: undefined }
> y = {a: 1}
{ a: 1 }
> x.a
1
> y.a
1
> x.b
undefined
> y.b
undefined
> Object.keys(x)
[ 'a', 'b' ]
> Object.keys(y)
[ 'a' ]
> JSON.stringify(x)
'{"a":1}'
> JSON.stringify(y)
'{"a":1}'
It appears the author is saying that if you want to write good, responsible code, you should not use undefined as a property value, because you could asking for confusion.
But semantically, that undefined is really there, so if you interpret what the author is saying literally, it's technically incorrect. But if you follow best practices, I can see what he's trying to get across. It's very Crockford-esque.
By the way, the right way to remove properties is with delete but that's perhaps for a different question.
ADDENDUM
In response to a question in the comments from @prsvr, I looked up the old ECMAScript 3 Specification and found the following:
ECMAScript is object-based: basic language and host facilities are provided by objects, and an ECMAScript program is a cluster of communicating objects. An ECMAScript object is an unordered collection of properties each with zero or more attributes that determine how each property can be used—for example, when the ReadOnly attribute for a property is set to true, any attempt by executed ECMAScript code to change the value of the property has no effect. Properties are containers that hold other objects, primitive values, or methods. A ECMAScript Language primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, and String; an object is a member of one of the following built-in types: Undefined, Null, Boolean, Number, and String; an object is a member of the remaining built-in type Object; and a method is a function associated with an object via a property.
There is no mention of undefined being disallowed as property values in the spec. (https://www-archive.mozilla.org/js/language/E262-3.pdf).
There were changes to undefined during the evolution of JavaScript; for example you used to be able to reassign the value of the identifier undefined to some value other than undefined, hence the idiom typeof(x) === "undefined" (among others).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With