简体   繁体   English

Javascript混淆null,instanceof和typeof的语法不一致?

[英]Javascript confusing syntax inconsistence for null, instanceof and typeof?

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. 我已经阅读了很多关于SO的相关主题,但仍然无法得到这个。

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 ? 理解'typeof'会返回一个String,所以它几乎反映了Javascript中的规则。(例如,null是一个对象......好吧..)但是为什么“null instanceof Object”返回false?

"x instanceof y" “x instanceof y”

Does it mean 'x' has to be created by the 'y' constructor? 这是否意味着'x'必须由'y'构造函数创建? And for null this is not the case ? 对于null,情况并非如此?

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 null和undefined之间的区别

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

REF REF

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

It's just the design decision which might be contrived or weird. 这只是设计决策,可能是人为的或奇怪的。 According to the typeof UnaryExpression if evaluated as the following. 按照typeof UnaryExpression如果评价为以下。 I've just included the poin that matters. 我刚才包括重要的点。

ECMA Spec: Return a String determined by Type(val) according to Table 20. ECMA规范:根据表20返回由Type(val)确定的字符串。

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

So, there's nothing we can do about it. 所以,我们无能为力。 It's . 这是 But it's correct to return false because, there is a separate type for null called Null type 但返回false是正确的,因为null有一个单独的类型,称为Null type

Null type: type whose sole value is the null value Null type:唯一值为空值的类型

null isn't an instance of Object, obviously, since it has got it's own type. null显然不是Object的实例,因为它有自己的类型。 It's just that typeof operator returns "object" . 只是typeof运算符返回"object" It's got to do with the design of javascript. 这与javascript的设计有关。

Why is it so? 为什么会这样? Will have to ask Brendan Eich(Founder of Javascript). 将不得不问Brendan Eich(Javascript的创始人)。

That's a common bug of ECMAScript. 这是ECMAScript的常见错误。

null is not an object , it's a primitive value.(So you can't modify it like adding properties to it) null不是一个object ,它是一个原始值。(所以你不能修改它就像添加属性一样)

typeof null should return null typeof null应该返回null

typeof null // object (bug in ECMAScript, should be null) typeof null // object(ECMAScript中的bug,应为null)

typeof undefined // undefined typeof undefined // undefined

null === undefined // false null === undefined // false

null == undefined // true null == undefined // true

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

EDITED EDITED

It's not available to see though 虽然看不到它

Changed 3 weeks ago by brendan 由brendan在3周前更改

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. 你知道,这一切都是因为1995年5月初的匆忙而导致的,这导致了null和对象类型共享的类型标记表示的泄漏。 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". 但是null意味着“没有对象”,所以它没有引起骚动,直到在Netscape 2中修复为时已晚,之后我们不愿意“修复”它并“破坏网络”。 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. 让我们不要为ES4更改typeof null并处理更重要的问题。

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

also check this answer 还检查这个答案

Why is null an object and what's the difference between null and undefined? 为什么null是一个对象,null和undefined之间有什么区别?

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 . instanceof更吸引人,但你检查instaceof undefined抛出异常,因为undefined不是任何单一类型的对象名,它可能是也可能不是JavaScript Object sub method Because undefined that type for null (Null Type). 因为undefined类型为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. 你应该检查undefined是instaceof JS对象,给它一个不是它不是对象。 It's give a boolean result. 它给出了一个boolean结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM