How to check if an argument is an object (and not an array) in JavaScript

后端 未结 3 810
既然无缘
既然无缘 2021-01-23 03:09

After testing out instasnceof I found that it will return true if the argument is an array or an object literal.

function test(options){
  if(option         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-23 03:38

    If you're trying to forbid arrays, you can just do this:

    var isObject = options instanceof Object;
    var isArray = options instanceof Array;
    if(isObject && !isArray)
    {
        alert('yes');
    }
    

提交回复
热议问题