oop - Javascript confusing syntax inconsistence for null, instanceof and typeof? - Stack Overflow

var obj = {};typeof obj;returns "object"obj instanceof Objectreturn truetypeof null

var obj = {};
typeof obj; // returns "object"
obj instanceof Object // return true

typeof null // returns "object"
null instanceof Object // returns false

And how about

typeof undefined // return "undefined"
undefined instanceof undefined 
// ERROR:  Uncaught TypeError: Expecting a function in instanceof check, 
//         but got undefined

Why is this the case? I have read a lot about related topics on SO but still can't get this.

Understand that 'typeof' would return a String, so it pretty much reflects the rules in Javascript.(eg. null is a object... well fine..) But why "null instanceof Object" return false ?

"x instanceof y"

Does it mean 'x' has to be created by the 'y' constructor? And for null this is not the case ?

EDIT

Would really appreciate if you could explain the different intention behind instanceof and typeof otherthan then syntax and return value.

Difference between null and undefined

typeof null        // object (bug in ECMAScript, should be null)
typeof undefined   // undefined
null === undefined // false
null  == undefined // true

REF

var obj = {};
typeof obj; // returns "object"
obj instanceof Object // return true

typeof null // returns "object"
null instanceof Object // returns false

And how about

typeof undefined // return "undefined"
undefined instanceof undefined 
// ERROR:  Uncaught TypeError: Expecting a function in instanceof check, 
//         but got undefined

Why is this the case? I have read a lot about related topics on SO but still can't get this.

Understand that 'typeof' would return a String, so it pretty much reflects the rules in Javascript.(eg. null is a object... well fine..) But why "null instanceof Object" return false ?

"x instanceof y"

Does it mean 'x' has to be created by the 'y' constructor? And for null this is not the case ?

EDIT

Would really appreciate if you could explain the different intention behind instanceof and typeof otherthan then syntax and return value.

Difference between null and undefined

typeof null        // object (bug in ECMAScript, should be null)
typeof undefined   // undefined
null === undefined // false
null  == undefined // true

REF

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/instanceof https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/typeof

Share Improve this question edited Mar 10, 2015 at 4:51 noooooooob asked Mar 10, 2015 at 4:13 noooooooobnoooooooob 1,8903 gold badges22 silver badges28 bronze badges 7
  • 1 The typeof operator is weird; there's not much else to it. However having null be an instance of Object would make very little sense, because a null reference clearly isn't an instance of anything. – Pointy Commented Mar 10, 2015 at 4:15
  • 1 null is just an exception to normal logic in this way. I have idea why the JS designers made it this way, but this is how it is so we live with it. for this reason, you often have to rule out x !== null before applying other tests. – jfriend00 Commented Mar 10, 2015 at 4:23
  • @jfriend00 I have idea why the JS designers made it this way - Really? I am curious. What is the reason? I thought it was a mistake. – thefourtheye Commented Mar 10, 2015 at 4:27
  • 1 @thefourtheye - sorry, I meant: "I have no idea why they made it that way". Typo in my ment that can't be edited any more. – jfriend00 Commented Mar 10, 2015 at 4:30
  • 1 I agree that it's essentially a wart in the language design, but it's understandable that typeof null would end up being object because a null means that a variable or property is not a primitive value and also explicitly not referencing some object. The spec discusses a Null type, of which null is the only value, but the purpose of null as a value is to be a reference to no object. It's kind-of a conundrum, and I don't know how much better things would be if typeof null were null. – Pointy Commented Mar 10, 2015 at 4:35
 |  Show 2 more ments

3 Answers 3

Reset to default 5

It's just the design decision which might be contrived or weird. According to the typeof UnaryExpression if evaluated as the following. I've just included the poin that matters.

ECMA Spec: Return a String determined by Type(val) according to Table 20.

Table 20:
╔═════════════╦══════════╗
║ Type of val ║  Result  ║
╠═════════════╬══════════╣
║ null        ║ "object" ║
╚═════════════╩══════════╝

So, there's nothing we can do about it. It's status-by-design. But it's correct to return false because, there is a separate type for null called Null type

Null type: type whose sole value is the null value

null isn't an instance of Object, obviously, since it has got it's own type. It's just that typeof operator returns "object". It's got to do with the design of javascript.

Why is it so? Will have to ask Brendan Eich(Founder of Javascript).

instanceof more appealing, however you checked instaceof undefined going throw exception that because the undefined is not any singular type object name, It's may or may not sub method of JavaScript Object. Because undefined that type for null (Null Type).

typeof undefined // return "undefined"
undefined instanceof Object // return False 

You should check undefined is instaceof JS object, that give a no it's not the object. It's give a boolean result.

That's a mon bug of ECMAScript.

null is not an object, it's a primitive value.(So you can't modify it like adding properties to it)

typeof null should return null

typeof null // object (bug in ECMAScript, should be null)

typeof undefined // undefined

null === undefined // false

null == undefined // true

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/null

EDITED

It's not available to see though

Changed 3 weeks ago by brendan

You know, this all came about because of rushing in early May 1995, which led to a leak of type tag representation shared by null and object types. But null means "no object", so it didn't raise hackles until it was too late to fix in Netscape 2, and after that we were loath to "fix" it and "break the web". That argument only applies more in degree of web population now. We have other fish to fry. This one was has been swallowed already. Let's not change typeof null for ES4 and work on more vital issues.

http://web.archive/web/20071110193102/http://bugs.ecmascript/ticket/250

also check this answer

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745408512a4626422.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信