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 thetypeof
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"
althoughnull
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
2 Answers
Reset to default 8By 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 thetypeof
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"
althoughnull
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
2 Answers
Reset to default 8By 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
版权声明:本文标题:javascript - Why can't a property be added to a null value? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745609509a2158920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论