最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Why can't a property be added to a null value? - Stack Overflow

matteradmin5PV0评论

If null value of javascript is an empty object so why can't add a property to it? the below code clears my question:

var a = null;

typeof a;
>>> "object"

a.name = 'name';
>>> TypeError: Cannot set property 'name' of null

var a = new Object();

typeof a;
>>> "object"

a.name = 'name';
>>> "name"

If null value of javascript is an empty object so why can't add a property to it? the below code clears my question:

var a = null;

typeof a;
>>> "object"

a.name = 'name';
>>> TypeError: Cannot set property 'name' of null

var a = new Object();

typeof a;
>>> "object"

a.name = 'name';
>>> "name"
Share Improve this question edited Jan 15, 2013 at 22:43 user166390 asked Jan 15, 2013 at 20:26 Mustafa ShujaieMustafa Shujaie 8162 gold badges11 silver badges18 bronze badges 4
  • null is not an "empty object", despite what the typeof operator evaluates to. – Phrogz Commented Jan 15, 2013 at 20:28
  • "I had to be done in ten days or something worse than JavaScript would have happened." - Brendan Eich – danronmoon Commented Jan 15, 2013 at 20:28
  • I think what may be confusing about this is that typeof null returns "object" although null is not actually an object. – dgvid Commented Jan 15, 2013 at 20:29
  • I really wish I could close this as a duplicate, but try as I might, I can only find "specific implementation errors" and not a similar general question. In any case, stackoverflow./questions/461966/… is an interesting read. – user166390 Commented Jan 15, 2013 at 22:43
Add a ment  | 

2 Answers 2

Reset to default 8

By definition neither the null value nor the undefined value have any properties, nor can any properties be added to them.

This is summarized nicely for null:

primitive value that represents the intentional absence of any object value.

And likewise, for undefined:

primitive value used when a variable has not been assigned a value.

(null is the only value of the Null-type and undefined is the only value of the Undefined-type.)

Now, for the implementation goodies:

Both of these types represent primitives and the behavior of "primitiveValue.Property" is covered by the internal ToObject method. (See GetValue/PutValue for the start of the rabbit hole.)

From 9.9: ToObject:

The abstract operation ToObject converts its argument to a value of type Object according to ..

  • Undefined => Throw a TypeError exception.
  • Null => Throw a TypeError exception.
  • (and so on)

As far as the ments, see 11.4.3: The typeOf Operator:

Return a String determined by Type(val) according to ..

  • Undefined => "undefined"
  • Null => "object"
  • (and so on)

null is an object in Javascript that represents the absence of an object. You cannot add a property to nothing.

See also: Why is null an object and what's the difference between null and undefined?

Post a comment

comment list (0)

  1. No comments so far