Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if an object has a given prototype?

Tags:

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.

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

like image 409
user1032531 Avatar asked Dec 15 '17 02:12

user1032531


People also ask

Does every object have a prototype?

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.

How do you get the prototype of an object?

The Object. getPrototypeOf() method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.

How do you check if an object has a method?

To check if an object has a method in TypeScript: Mark the specific method as an optional key in the object's type. Use typeof to check if accessing the method on the object returns a value with a function type. If the typeof operator returns true , the method exists.

How do you check if an object contains a property?

The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.


1 Answers

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

'URLSearchParams' in window

or

typeof window.URLSearchParams === 'function'

and alike will 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'));
}
like image 152
Kaiido Avatar answered Sep 21 '22 13:09

Kaiido