admin管理员组

文章数量:1024615

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?

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?

本文标签: javascriptWhy can39t a property be added to a null valueStack Overflow