Skip to content

Returns true if an object has a given property

License

Notifications You must be signed in to change notification settings

vikramcse/o-has

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

o-has

Returns true if an object has a given property

Install

$ npm install --save o-has

Example

var has = require('o-has');

var a = {
  name: 'vikram'
};

console.log(has(a, 'name'));
// -> true

Explination

In case we are using JavaScript objects for hashtable solution then, we can't rely on any of its inherited properties such as hasOwnProperty here is an example

var obj = {
	name: 'vikram',
	hasOwnProperty: 'dummy'
};

console.log(obj.hasOwnProperty('hasOwnProperty'));
// -> TypeError: Property 'hasOwnProperty' of object obj is not a function

In above example it tries to execute inherited hasOwnProperty and which not a function.

The workaround for this problem is to use Object.prototype hasOwnProperty method

require('o-has')(object, key)

Arguments:

  • object: Object
  • key: the key that to be checked

Returns: Returns true if an object has a given property

License

© 2016 vikram. MIT License

About

Returns true if an object has a given property

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published