189

How can I check if an anonymous object that was created as such:

var myObj = { 
    prop1: 'no',
    prop2: function () { return false; }
}

does indeed have a prop2 defined?

prop2 will always be defined as a function, but for some objects it is not required and will not be defined.

I tried what was suggested here: How to determine if Native JavaScript Object has a Property/Method? but I don't think it works for anonymous objects .

Philipp Kief
  • 8,172
  • 5
  • 33
  • 43
Omar
  • 39,496
  • 45
  • 145
  • 213
  • 7
    There's not much difference between anonymous and non-anonymous objects in JavaScript. They are the same under the cover. – vava Jun 09 '10 at 15:50

7 Answers7

341

typeof myObj.prop2 === 'function'; will let you know if the function is defined.

if(typeof myObj.prop2 === 'function') {
    alert("It's a function");
} else if (typeof myObj.prop2 === 'undefined') {
    alert("It's undefined");
} else {
    alert("It's neither undefined nor a function. It's a " + typeof myObj.prop2);
}
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
56

You want hasOwnProperty():

var myObj1 = { 
 prop1: 'no',
 prop2: function () { return false; }
}
var myObj2 = { 
 prop1: 'no'
}

console.log(myObj1.hasOwnProperty('prop2')); // returns true
console.log(myObj2.hasOwnProperty('prop2')); // returns false
 

References: Mozilla, Microsoft, phrogz.net.

Liam
  • 27,717
  • 28
  • 128
  • 190
artlung
  • 33,305
  • 16
  • 69
  • 121
  • 5
    this will not work if you have placed a dom node within a self-made object, eg if `var my_obj = {'node1': document.createElement('div')};` then `my_obj.node1.hasOwnProperty('cloneNode')` is false. however Sean's solution will correctly return true: `typeof my_obj.node1.cloneNode === 'function'` – mulllhausen Feb 24 '13 at 08:58
  • 11
    Won't work if you are looking for a method that was defined in the prototype. – Wilt Jan 26 '16 at 13:19
  • 2
    @Wilt question specified anonymous object literal. – artlung Jan 27 '16 at 16:36
  • 4
    @artlung Yes your answer is good for the question, just wanted to leave that comment here for others who might be looking for similar a solution working in other cases (like me)... Didn't mean to disqualify your answer. – Wilt Jan 27 '16 at 16:53
  • 2
    Doesn't handle the case where the property is defined, but is not a function. – matt2000 Aug 29 '18 at 23:42
25

3 Options

  1. typeof myObj.prop2 === 'function' if the property name is not dynamic/generated
  2. myObj.hasOwnProperty('prop2') if the property name is dynamic, and only check if it is direct property (not down the prototype chain)
  3. 'prop2' in myObj if the property name is dynamic, and check down the prototype chain
Peter Tseng
  • 13,613
  • 4
  • 67
  • 57
8

What do you mean by an "anonymous object?" myObj is not anonymous since you've assigned an object literal to a variable. You can just test this:

if (typeof myObj.prop2 === 'function')
{
    // do whatever
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
4

I know this is an old question, but I am surprised that all answers ensure that the method exists and it is a function, when the OP does only want to check for existence. To know it is a function (as many have stated) you may use:

typeof myObj.prop2 === 'function'

But you may also use as a condition:

typeof myObj.prop2

Or even:

myObj.prop2

This is so because a function evaluates to true and undefined evaluates to false. So if you know that if the member exists it may only be a function, you can use:

if(myObj.prop2) {
  <we have prop2>
}

Or in an expression:

myObj.prop2 ? <exists computation> : <no prop2 computation>
Javier Elices
  • 2,066
  • 1
  • 16
  • 25
1

One way to do it must be if (typeof myObj.prop1 != "undefined") {...}

Ain Tohvri
  • 2,987
  • 6
  • 32
  • 51
1

It's better to use Object.prototype.hasOwnProperty.call(obj, method)

In ECMAScript 5.1, Object.create was added, which enables the creation of objects with a specified [[Prototype]]. Object.create(null) is a common pattern used to create objects that will be used as a Map. This can lead to errors when it is assumed that objects will have properties from Object.prototype. This rule prevents calling some Object.prototype methods directly from an object.

Additionally, objects can have properties that shadow the builtins on Object.prototype, potentially causing unintended behavior or denial-of-service security vulnerabilities. For example, it would be unsafe for a webserver to parse JSON input from a client and call hasOwnProperty directly on the resulting object, because a malicious client could send a JSON value like {"hasOwnProperty": 1} and cause the server to crash.

To avoid subtle bugs like this, it's better to always call these methods from Object.prototype. For example, foo.hasOwnProperty("bar") should be replaced with Object.prototype.hasOwnProperty.call(foo, "bar").

const myObj = { 
  prop1: 'no',
  prop2: function () { return false }
}

console.log(Object.prototype.hasOwnProperty.call(myObj, 'prop2'))
console.log(Object.prototype.hasOwnProperty.call(myObj, 'prop3'))
dreamLo
  • 1,612
  • 12
  • 17