首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

Js检测数据类型

原理 console.log([] instanceof Object) //true 例如上面这个检测过程 我们可以理解为 [].prototype => Array.prototype => Object.prototype...数组的原型会指向Array.prototype,间接的指向了Object.prototype,所以[]也属于对象返回正确,所以说instanceof判断的是两个对象是否属于实例关系,而不会直接返回数据类型...] console.log(Object.prototype.toString.call(arr)) //[object Array] console.log(arr.toString()) // 1,2,3...这里也涉及到原型链的知识,我们分析一下: 首先arr.toString调用的是Array.prototype上面的toSting(),虽然Array也继承与Object但是这个方法在Array上进行了重写...包装类也是对象,既然都是对象为什么调用toString()返回结果不一样?

2.9K40

关于数组的前端面试题,你是否都能答对?

你知道Array.prototype的类型是什么吗? 如何“打平”一个嵌套数组,如[1,[2,[3]],4,[5]] => [1,2,3,4,5]?你能说出多少种方法? 如何克隆一个数组?...为什么不用typeof? Array继承与Object,所以typeof 会直接返回object,所以不可以用typeof方法来检测 为什么不用instanceof?...var list = [1,2,3]; list instanceof Array //true instanceof 表面上看确实是返回了true,但其实并不可靠。...为什么不用constructor方法? var list = [1,2,3]; list.constructor === Array; //true 原因已经解释过了,不再赘述。...很多人都不知道,其实Array.prototype是一个数组,只不过length为0 如何“打平”一个嵌套数组,如[1,[2,[3]],4,[5]] => [1,2,3,4,5]?

1.3K30

奇怪的Java题:为什么128 == 128返回false,而127 == 127会返回为true?

奇怪的Java题:为什么128 == 128返回false,而127 == 127会返回为true? 在回答这个问题之前,我们先来看看int和Integer的对比,一步步揭开问题的答案。...Integer i = new Integer(100); Integer j = new Integer(100); System.out.print(i == j); //false 因为new生成的是两个对象...Integer和基本数据类型int比较时,java会自动拆包装为int,然后进行比较,实际上就变为两个int变量的比较 (3) 非new生成的Integer变量和new Integer()生成的变量比较时,结果为false...Integer i = new Integer(100); Integer j = 100; System.out.print(i == j); //false 在JDK 5.0之前,你从未见过Integer...127 之外的数 Integer m = 128; Integer n = 128; System.out.println( m==n ); //false

2.2K31
领券