最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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(options instanceof Object){alert('yes')}//this will alert for both arrays and object literals
}
test({x:11})// alerts
test([11])// alerts as well but I do not want it to

Is there a way to test if the argument "options" is an object literal?

P.S. I am creating a module that will allow the user to access its configuration options, and I want to test if the argument is only an object literal or not?

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(options instanceof Object){alert('yes')}//this will alert for both arrays and object literals
}
test({x:11})// alerts
test([11])// alerts as well but I do not want it to

Is there a way to test if the argument "options" is an object literal?

P.S. I am creating a module that will allow the user to access its configuration options, and I want to test if the argument is only an object literal or not?

Share Improve this question edited Apr 7, 2015 at 6:09 Mahdi Alkhatib 1,9821 gold badge32 silver badges43 bronze badges asked May 24, 2012 at 16:31 zerozero 3,0849 gold badges45 silver badges70 bronze badges 2
  • 1 "Object literal" is just a syntax for creating objects. I assume you're trying to specifically distinguish between an Array object and a non-Array object. I'm certain this has been covered on SO. – user1106925 Commented May 24, 2012 at 16:33
  • 1 Distinguish between array and hash in javascript with typeof() – user1106925 Commented May 24, 2012 at 16:36
Add a ment  | 

4 Answers 4

Reset to default 12

is there a way to test if the argument "options" is an object literal?

No, because it makes no sense. You can test whether it's an object, but how it was created (via a literal in the call to your function, via a literal elsewhere, through new Object, by deserializing a JSON string, ...) is not information that's maintained.

after testing out instasnceof i found that it will return true if the argument is an array or an object literal

Correct. Arrays in JavaScript are objects (and not really arrays).

If you want to test that an object is a plain old object, you can do this:

if (Object.prototype.toString.call(options) === "[object Object]") {
    // It's a plain object
}

But there's really no reason to do that. It's not your problem. As long as what they pass you has the properties you expect, don't try to limit the object further.

p.s. i'm making a module that will allow the user to pass it configuration options and i want to test to make sure that the argument is only an object literal.

Why? If the user wants to use an object that hasn't been declared as a literal right there and then, why would you care? If they want to use an object that they've created via a different constructor function (e.g., rather than just a plain object), again, why would you care?

function isPlainObject(o) {
     return Object(o) === o && Object.getPrototypeOf(o) === Object.prototype;
}

However, you can't test wether o was declared as a literal or instantiated somehow else - you can just test whether it's a plain object without any constructor than Object.

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');
}

An alternative solution would be to use Lodash:

_.isPlainObject(value)

Here is the documentation: https://lodash./docs/4.17.15#isPlainObject

发布评论

评论列表(0)

  1. 暂无评论