我是靠谱客的博主 含蓄墨镜,这篇文章主要介绍《Javascript秘密花园》学习笔记(终),现在分享给大家,希望可以做个参考。

实践了许多代码后,回头再看《Javascript秘密花园》,又有一些收获,与诸君分享

数组

typeof运算符

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Value Class Type ------------------------------------- "foo" String string new String("foo") String object 1.2 Number number new Number(1.2) Number object true Boolean boolean new Boolean(true) Boolean object new Date() Date object new Error() Error object [1,2,3] Array object new Array(1, 2, 3) Array object new Function("") Function function /abc/g RegExp object (function in Nitro/V8) new RegExp("meow") RegExp object (function in Nitro/V8) {} Object object new Object() Object object

可以看出,typeof运算符主要有这些值:object,string,number,function.使用new关键字创建的对象除去function外typeof运算符都会得到object.但在判断string和number之间很好用,尤其是数组中的元素是否为string或number

复制代码
1
2
3
4
5
6
7
8
//给出一个数组 var test = [1,'1',true,new Function(""),'asd',function(){}]; typeof test[0]//number typeof test[1]//string typeof test[2]//boolean typeof test[3]//function typeof test[4]//string typeof test[5]//function

由上段代码可以看出typeof运算符在对非new关键字创建的元素类型【尤其是数组元素类型】判断上很好用。注意1和‘1’的判断。
所以要判断数组中的元素是否为数值类型(Number),可以这样写:

复制代码
1
2
3
4
var test = [12,'123',123,'2222','agc']; for(var i=0,len=test.length;i<len;i++){ console.log(typeof test[i]) }

转载于:https://www.cnblogs.com/xihe/p/6138618.html

最后

以上就是含蓄墨镜最近收集整理的关于《Javascript秘密花园》学习笔记(终)的全部内容,更多相关《Javascript秘密花园》学习笔记(终)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(101)

评论列表共有 0 条评论

立即
投稿
返回
顶部