Verify that a method or property exists in a js object

1. Take window as an example, enter window in the console

2. The verification method takes WebSocket as an example

  1. if (window.WebSocket) {
  2. console.log('WebSocket');
  3. }
  1. if (typeof(window.WebSocket) === 'function') {
  2. console.log('WebSocket');
  3. }
  1. if (window.WebSocket instanceof Function) {
  2. console.log('WebSocket');
  3. }
window.hasOwnProperty('WebSocket'); // true

3. Verify properties

'BCSE_VS' in window // true
window.hasOwnProperty('BCSE_VS'); // true

 

Related: Verify that a method or property exists in a js object