How to check if anonymous object has a method?

Javascript

Javascript Problem Overview


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: https://stackoverflow.com/questions/595766/how-to-determine-if-native-javascript-object-has-a-property-method but I don't think it works for anonymous objects .

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

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

Solution 4 - Javascript

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
}

Solution 5 - Javascript

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>

Solution 6 - Javascript

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

Solution 7 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOmarView Question on Stackoverflow
Solution 1 - JavascriptSean VieiraView Answer on Stackoverflow
Solution 2 - JavascriptartlungView Answer on Stackoverflow
Solution 3 - JavascriptPeter TsengView Answer on Stackoverflow
Solution 4 - JavascriptMatt BallView Answer on Stackoverflow
Solution 5 - JavascriptJavier ElicesView Answer on Stackoverflow
Solution 6 - JavascriptAin TohvriView Answer on Stackoverflow
Solution 7 - JavascriptdreamLoView Answer on Stackoverflow