function Foo() {}
function Bar() {}
Bar.prototype = new Foo();
new Bar() instanceof Bar; // true
new Bar() instanceof Foo; // true
// 如果仅仅设置 Bar.prototype 为函数 Foo 本省,而不是 Foo 构造函数的一个实例
Bar.prototype = Foo;
new Bar() instanceof Foo; // false
`instanceof` 比较内置类型(Using `instanceof` with native types)
new String(''foo'') instanceof String; // true
new String(''foo'') instanceof Object; // true
''foo'' instanceof String; // false
''foo'' instanceof Object; // false
有一点需要注意,instanceof 用来比较属于不同 JavaScript 上下文的对象(比如,浏览器中不同的文档结构)时将会出错,因为它们的构造函数不会是同一个对象。
结论(In conclusion)
instanceof 操作符应该仅仅用来比较来自同一个 JavaScript 上下文的自定义对象。正如 `typeof` 操作符一样,任何其它的用法都应该是避免的。
JavaScript "new Array(n)" 和 "Array.prototype.map" 怪异
我在 Firefox-3.5.7/Firebug-1.5.3 和 Firefox-3.6.16/Firebug-1.6.2 中观察到了这一点
当我启动 Firebug 时:
var x = new Array(3)
console.log(x)
// [undefined,undefined,undefined]
var y = [undefined,undefined]
console.log(y)
// [undefined,undefined]
console.log( x.constructor == y.constructor) // true
console.log(
x.map(function() { return 0; })
)
// [undefined,undefined]
console.log(
y.map(function() { return 0; })
)
// [0,0]
这里发生了什么?这是一个错误,还是我误解了如何使用new Array(3)
?

JavaScript typeof, null, 和 undefined的相关知识
JavaScript typeof, null, 和 undefined这些操作符在js中起到很重要的作用,让我们来详细的讲解下吧。
typeof 操作符
你可以使用 typeof 操作符来检测变量的数据类型。
实例
typeof "John" // 返回 string
typeof 3.14 // 返回 number
typeof false // 返回 boolean
typeof [1,2,3,4] // 返回 object
typeof {name:'John', age:34} // 返回 object
登录后复制
在JavaScript中,数组是一种特殊的对象类型。 因此 typeof [1,2,3,4] 返回 object。
立即学习“Java免费学习笔记(深入)”;
null
在 JavaScript 中 null 表示 "什么都没有"。
null是一个只有一个值的特殊类型。表示一个空对象引用。
用 typeof 检测 null 返回是object。
你可以设置为 null 来清空对象:
实例
var person = null; // 值为 null(空), 但类型为对象
登录后复制
你可以设置为 undefined 来清空对象:
实例
var person = undefined; // 值为 undefined, 类型为 undefined
登录后复制
undefined
在 JavaScript 中, undefined 是一个没有设置值的变量。
typeof 一个没有值的变量会返回 undefined。
实例
var person; // 值为 undefined(空), 类型是undefined
登录后复制
任何变量都可以通过设置值为 undefined 来清空。 类型为 undefined.
实例
person = undefined; // 值为 undefined, 类型是undefined
登录后复制
undefined 和 null 的区别
实例
null 和 undefined 的值相等,但类型不等:
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true
登录后复制
本文详细的讲解了JavaScript typeof, null, 和 undefined的相关知识,更多的学习资料清关注php中文网即可观看。
相关推荐:
JavaScript Date(日期) 相关知识与用法
JavaScript RegExp 对象的使用介绍
关于JavaScript Array(数组) 对象的使用方法
以上就是JavaScript typeof, null, 和 undefined的相关知识的详细内容,更多请关注php中文网其它相关文章!
![javascript typeof的用法与typeof运算符介绍[详细]_javascript技巧 javascript typeof的用法与typeof运算符介绍[详细]_javascript技巧](http://www.gvkun.com/zb_users/upload/2025/03/766be34a-dd7d-4f87-a060-783713c8d5951741164647251.jpg)
javascript typeof的用法与typeof运算符介绍[详细]_javascript技巧
下面是对于typeof运算符的详细介绍跟typeof的一些用法,分析,学习typeof的朋友,看完了,这篇应该能有所收获。
经常会在js里用到数组,比如 多个名字相同的input, 若是动态生成的, 提交时就需要判断其是否是数组.
if(document.mylist.length != "undefined" ) {} 这个用法有误.
正确的是 if( typeof(document.mylist.length) != "undefined" ) {}
或 if( !isNaN(document.mylist.length) ) {}
typeof的运算数未定义,返回的就是 "undefined".
运算数为数字 typeof(x) = "number"
字符串 typeof(x) = "string"
布尔值 typeof(x) = "boolean"
对象,数组和null typeof(x) = "object"
函数 typeof(x) = "function"
typeof 运算符返回一个用来表示表达式的数据类型的字符串。
可能的字符串有:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"。
如:
alert(typeof (123));//typeof(123)返回"number"
alert(typeof ("123"));//typeof("123")返回"string"
typeof 运算符
返回一个用来表示表达式的数据类型的字符串。
typeof[()expression[]] ;
expression 参数是需要查找类型信息的任意表达式。
说明
typeof 运算符把类型信息当作字符串返回。typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."
typeof 语法中的圆括号是可选项。
typeof运算符介绍:
typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。
它返回值是一个字符串,该字符串说明运算数的类型。
你知道下面typeof运算的结果吗?
typeof(1);
typeof(NaN);
typeof(Number.MIN_VALUE);
typeof(Infinity);
typeof("123");
typeof(true);
typeof(window);
typeof(document);
typeof(null);
typeof(eval);
typeof(Date);
typeof(sss);
typeof(undefined);
看看你会几个?
如果看了以后,不是很明白的话,请看下面(明白的人就不用往下看了):
typeof是一个一元运算符,它返回的结果始终是一个字符串,对不同的操作数,它返回不同的结果。
具体的规则如下:
一、对于数字类型的操作数而言, typeof 返回的值是 number。比如说:typeof(1),返回的值就是number。
上面是举的常规数字,对于非常规的数字类型而言,其结果返回的也是number。比如typeof(NaN),NaN在
JavaScript中代表的是特殊非数字值,虽然它本身是一个数字类型。
在JavaScript中,特殊的数字类型还有几种:
Infinity 表示无穷大特殊值
NaN 特殊的非数字值
Number.MAX_VALUE 可表示的最大数字
Number.MIN_VALUE 可表示的最小数字(与零最接近)
Number.NaN 特殊的非数字值
Number.POSITIVE_INFINITY 表示正无穷大的特殊值
Number.NEGATIVE_INFINITY 表示负无穷大的特殊值
以上特殊类型,在用typeof进行运算进,其结果都将是number。
二、对于字符串类型, typeof 返回的值是 string。比如typeof("123")返回的值是string。
三、对于布尔类型, typeof 返回的值是 boolean 。比如typeof(true)返回的值是boolean。
四、对于对象、数组、null 返回的值是 object 。比如typeof(window),typeof(document),typeof(null)返回的值都是object。
五、对于函数类型,返回的值是 function。比如:typeof(eval),typeof(Date)返回的值都是function。
六、如果运算数是没有定义的(比如说不存在的变量、函数或者undefined),将返回undefined。比如:typeof(sss)、typeof(undefined)都返回undefined。
<script> <br/>document.write ("typeof(1): "+typeof(1)+"<br>"); <br/>document.write ("typeof(NaN): "+typeof(NaN)+"<br>"); <br/>document.write ("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br>") <br/>document.write ("typeof(Infinity): "+typeof(Infinity)+"<br>") <br/>document.write ("typeof(\"123\"): "+typeof("123")+"<br>") <br/>document.write ("typeof(true): "+typeof(true)+"<br>") <br/>document.write ("typeof(window): "+typeof(window)+"<br>") <br/>document.write ("typeof(document): "+typeof(document)+"<br>") <br/>document.write ("typeof(null): "+typeof(null)+"<br>") <br/>document.write ("typeof(eval): "+typeof(eval)+"<br>") <br/>document.write ("typeof(Date): "+typeof(Date)+"<br>") <br/>document.write ("typeof(sss): "+typeof(sss)+"<br>") <br/>document.write ("typeof(undefined): "+typeof(undefined)+"<br>") <br/></script>
我们今天的关于JavaScript中 奇怪的typeof解析 和Null, Array的分享已经告一段落,感谢您的关注,如果您想了解更多关于Array, Array Constructor, for in loop, typeof, instanceOf_javascript技巧、JavaScript "new Array(n)" 和 "Array.prototype.map" 怪异、JavaScript typeof, null, 和 undefined的相关知识、javascript typeof的用法与typeof运算符介绍[详细]_javascript技巧的相关信息,请在本站查询。