null instanceof object
2017-07-29 10:29
232 查看
转载地址:http://bbs.csdn.net/wap/topics/370040293
null instanceof object
instanceof 判断,表示某个变量是否是某个对象的实例 。
这是由Javascript规范规定的,Null和Object都是javascript中的数据类型。Null数据类型只有一个值:null。就像undefined数据类型只有一个值:undefined。问题出在typeof操作符的定义规范,如下:
11.4.3 The typeof Operator
The production UnaryExpression : typeof UnaryExpression is evaluated as follows:
1. Evaluate UnaryExpression.
2. If Type(Result(1)) is not Reference, go to step 4.
3. If GetBase(Result(1)) is null, return "undefined".
4. Call GetValue(Result(1)).
5. Return a string determined by Type(Result(4)) according to the following table:
Type Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Object (native and
doesn’t implement
[[Call]])
"object"
Object (native and
implements [[Call]])
"function"
Object (host) Implementation-dependent
可以看到,对于Null类型的值(只有null),规范就是定义返回"object"这个字符串。但是本质上Null和Object不是一个数据类型,null值并不是以Object为原型创建出来的。所以null instanceof Object是false。但从这里也可以看到,null确实是javascript中用来表示空引用的一个特殊值。使得它不是instanceof Ojbect,而typeof null是“object”。在语义上也是可以理解的。
null instanceof object
instanceof 判断,表示某个变量是否是某个对象的实例 。
这是由Javascript规范规定的,Null和Object都是javascript中的数据类型。Null数据类型只有一个值:null。就像undefined数据类型只有一个值:undefined。问题出在typeof操作符的定义规范,如下:
11.4.3 The typeof Operator
The production UnaryExpression : typeof UnaryExpression is evaluated as follows:
1. Evaluate UnaryExpression.
2. If Type(Result(1)) is not Reference, go to step 4.
3. If GetBase(Result(1)) is null, return "undefined".
4. Call GetValue(Result(1)).
5. Return a string determined by Type(Result(4)) according to the following table:
Type Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Object (native and
doesn’t implement
[[Call]])
"object"
Object (native and
implements [[Call]])
"function"
Object (host) Implementation-dependent
可以看到,对于Null类型的值(只有null),规范就是定义返回"object"这个字符串。但是本质上Null和Object不是一个数据类型,null值并不是以Object为原型创建出来的。所以null instanceof Object是false。但从这里也可以看到,null确实是javascript中用来表示空引用的一个特殊值。使得它不是instanceof Ojbect,而typeof null是“object”。在语义上也是可以理解的。
相关文章推荐
- Unity: “NullReferenceException: Object reference not set to an instance of an object”的可能解決方法
- NullReferenceException: Object reference not set to an instance of an object
- NullReferenceException: Object reference not set to an instance of an object
- 关于Unity3D的错误:NullReferenceException: Object reference not set to an instance of an object 的解答
- VS2005 solution文件打开时弹出“object reference not set to an instance of an object”错误的解决方法
- 添加wcf服务时弹出"an object reference not set to an instance of an object "
- type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object
- Object reference not set to an instance of an object.
- [Bug]Object reference not set to an instance of an object.
- java反射错误:object is not an instance of declaring class
- Attempt to set model object on null model of component
- 报错:object is not an instance of declaring class
- 我遇到的Object reference not set to an instance of an object.错误的解决办法
- [BTS] Error biztalk arguments null exception string reference not set to an instance of a string. parameter name
- Object reference not set to an instance of an object
- Object instanceof Function 还是 Function instance of Object,是真是假,一一道来
- ASP.NET MVC Html.TextBox 报 Object reference not set to an instance of an object 的解决方案
- 反射获取方法时:object is not an instance of declaring class
- object is not an instance of declaring class while invoking public abstract…的解决
- java axis调用cxf 的坑object is not an instance of declaring class while invoking public