1.如何判断数组
js如何判断一个变量是数组
// ES6中增加的数组方法
Array.isArray()
// 使用constructor判断
function isArray(arr) {
return arr.constructor.toString().indexOf("Array") > -1;
}
function isArray(arr) {
return arr.constructor === Array;
}
// 用instanceof判断
function isArray(arr){
return arr instanceof Array;
}
6、js类型的隐式转换
9、代码执行结果
[] + []
[] + ![]
[] == ![]
[] == []
1.结果是",空字符串,因为数组是对象类型,先转为字符串在进行加法运算
2.结果是‘false’,因为这个表达式等同于[]+false,对象转为字符串即’‘+false
3.结果是true,这个相对复杂,首先这个表达式等同于[]==false,然后布尔转为数字:[]==0,然后对象要转为字符串再比较,即:’'0,这样是一个字符串和一个数值比较,要先将字符串转为数字,即:00
4.是false,因为不是同一个对象的引用。
10、代码执行结果
null == 0
null > 0
null < 0
null >= 0
null <= 0
参考答案
null==0是false,因为null只和自己或者undefined相等,其他都是false null和0进行大小比较时候,因为0是数值类型,因此将null也转为数值进行比较,所以null>0和null<0为false,,而null>=0和null<=0为true
11、代码执行结果
let num = 10;
function ch(num) {
num = 12;
}
ch(num);
console.log(num);
let obj = {};
function ch1(obj) {
obj.a = 'a';
}
ch1(obj);
console.log(obj.a)
参考答案
num打印结果是10,因为num是值类型,在ch函数作用域内是临时变量,不会影响传入的实参。
obj1.a打印结果是‘a’,因为obj是引用类型,传入ch1中obj指向外部的obj数据,因此函数内的代码直接改变了obj的属性。
12、类型判断
typeof判断哪个类型会出错?
typeof判断null和数组会出错
Object.prototype.toString.call()判断哪个类型会出错?
Object.prototype.toString.call()判断自定义对象只能得到’[object object]’ 的结果,所以如果需要判断构造函数可以结合instanceof或者constructor来使用
13、typeof能判断函数和null吗
typeof能判断函数,不能判断null
14、如何判断一个对象为空
// 方法1 注意该方法性能较差
function isEmptyObject(obj) {
return JSON.stringify(obj) === '{}';
}
// 方法2 判断是否含有私有属性
function isEmptyObject(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
// 方法3 Object.keys也是只能获取自身属性,不能获取原型属性
function isEmptyObject(obj) {
return Object.keys(obj).length === 0;
}
15.typeof和instanceof的区别
参考答案:typeof和instanceof都是js中用来判断数据类型的方法
typeof适用于判断数据基本类型的场景,instanceof用来判断对象(包括内置对象和自定义对象)所对应的类的场景。
typeof用来查看字面量或者变量的数据类型
typeof 1 // 'number'
typeof '1' // 'string'
typeof false // 'boolean'
typeof {} // 'object'
typeof [] // 'object'
typeof new Date() // 'object'
typeof (() => {}) // 'function'
typeof undefined // 'undefined'
typeof Symbol(1) // 'symbol'
由结果可知typeof可以测出number string Boolean symbol undefined 以及 function,而对于null及数组 对象,typeof均检测出为object,不能进一步判断它们的类型。
instanceof可以判断一个对象的构造函数是否等于给定的值
({}) instanceof Object // true
[] instanceof Array // true
new Date() instanceof Date // true
/123/g instanceof RegExp // true
function Person() {}
const p = new Person();
p instanceof Person // true
p instanceof Object // true
instanceof原理是判断构造函数的原型prototype属性是否出现在对象的原型链上。
需要注意的是,这里提到instanceof是判断对象的构造函数,不适用与非对象类型的变量,看MDN的例子:
let literalString = 'This is a literal string';
let stringObject = new String('String created with constructor');
literalString instanceof String; // false, string literal is not a String
stringObject instanceof String; // true
constructor
console.log(false.constructor === Boolean);// true
console.log((1).constructor === Number);// true
console.log(''.constructor === String);// true
console.log([].constructor === Array);// true
console.log(({}).constructor === Object);// true
console.log((function test() {}).constructor === Function);// true
console.log(Symbol('1').constructor === Symbol);// true
undefined和null没有contructor属性
这里可以看到虽然数字1的构造函数是number,但1是对象字面量,不是通过new创建的,因此使用instanceof判断为false。
Object.prototype.toString
object是js中所有其他数据类型的父类。意思是所有的数据类型都继承了object。但是无论是string还是array都是会重写这个tostring方法的。所以’1’.toString()和Object.toString.call(‘1’)的结果不同。
function Test(){};
const t = new Test();
Object.prototype.toString.call(1); '[object Number]'
Object.prototype.toString.call(NaN); '[object Number]'
Object.prototype.toString.call('1'); '[object String]'
Object.prototype.toString.call(true); '[object Boolean]'
Object.prototype.toString.call(undefined); '[object Undefined]'
Object.prototype.toString.call(null); '[object Null]'
Object.prototype.toString.call(Symbol());'[object Symbol]'
Object.prototype.toString.call(Test); '[object Function]'
Object.prototype.toString.call([1,2,3]); '[object Array]'
Object.prototype.toString.call({});'[object Object]'
Object.prototype.toString.call(t);'[object Object]'
注意自定义对象的判断只能得到"[object Object]"的结果。
16null和undefined的区别
参考答案:本身都表示”没有“,但null表示引用类型的对象为空,undefined则表示变量未定义。
在相等判断时候,null和undefined是相等的。
但null和undefined在很多方面都有区别。
含义不同
null表示对象空指针,undefined表示变量未定义。
类型不同
typeof null // 'object'
typeof undefined // 'undefined'
Number(null) // 0
Number(undefined) // NaN
应用场景不同
null:作为对象原型链的终点
undefined:定义了变量,没有初始化,默认是undefined
函数不return,或者return后面没有值,则函数默认返回undefined
函数参数如果不传,默认是undefined。
17 0.1+0.2!=0.3的原因
参考答案:JavaScript对于小数数据使用双精度存储,10进制的小数转为二进制时候会损失一部分精度,因此再进行运算的话结果会不准确。
10进制的小数转二进制的方法是按位乘2并保留整数部分,由于双精度是按53存储,如果超过这个长度会四舍五入。
18 number的最大值
number类型最大值是多少?如果后台发的数据超过这个值怎么办?
大多数浏览器最大值是2^53,
最小值是-2^53。最大值和最小值可以通过Number.MAX_VALUE 和 Number.MIN_VALUE查看。
后台发送数据超过这个值可以用字符串类型代替。
19 12和12.0有什么区别?
参考答案:没有区别,整型值存储空间是浮点类型的一半,所以js会把12.0转为12存储。