如何判断对象是否有给定的原型? [英] How to tell if an object has a given prototype?

查看:128
本文介绍了如何判断对象是否有给定的原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测给定浏览器是否具有 URL searchParams 原型? https://developer.mozilla.org/en-US/ docs / Web / API / URL / searchParams 声明Chrome和FF可以,但Edge没有,但我希望使用JavaScript进行检测。

How can one detect if a given browser has the searchParams prototype for URL? https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams states that Chrome and FF do but Edge does not, but I wish to detect using JavaScript.

我已经与 isPrototypeOf 混淆,但不认为它适用。

I've messed around with isPrototypeOf, but don't think it is applicable.

推荐答案

在支持浏览器方面,将有一个 URLSearchParams 构造函数可用于全局对象,因此与任何其他全局构造函数一样,

In supporting browsers, there will be an URLSearchParams constructor available on global object, so like any other global Constructor,

'URLSearchParams' in window

typeof window.URLSearchParams === 'function'

等等do。

const support = typeof window.URLSearchParams === 'function';
console.log('supports URLSearchParams API:', support);

var url = new URL('https://stackoverflow.com/questions/47824782/how-to-tell-if-an-object-has-a-given-prototype?support="true"');
if(support){
    console.log(url.searchParams.get('support'));
}

这篇关于如何判断对象是否有给定的原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆