老师,我发现按照课件中的instanceof关键字方法判断URLSearchParams类型在运行时会有如下报错:
Uncaught (in promise) TypeError: Right-hand side of ‘instanceof’ is not an object
但是改成:
toString.call(val) === '[object URLSearchParams]'
就没有问题了,这是什么原因呀?
import { URLSearchParams } from "url";
// 报错的写法
export function isURLSearchParams(val: any): val is URLSearchParams {
return typeof val !== 'undefined' && val instanceof URLSearchParams;
}
//没有报错的写法
export function isURLSearchParams(val: any): val is URLSearchParams {
return typeof val !== 'undefined' && toString.call(val) === '[object URLSearchParams]';
}
具体报错:
Uncaught (in promise) TypeError: Right-hand side of 'instanceof' is not an object