0

hasOwnProperty is long and makes my code unreadable with long chained if statements.

Is there a way to rename hasOwnProperty to 'hop', 'has' or just 'h' so i can say something like

 if(req.body.h('first_name') && req.body.h('last_name'))
 {
    //....
 }
user1709076
  • 2,538
  • 9
  • 38
  • 59
  • Do you *need* `hasOwnProperty`? Because you can just use `in` but that also checks the prototype chain `'first_name' in req.body` – VLAZ Apr 02 '19 at 10:33
  • You can just do `if(req.body.first_name && req.body.last_name)` – ponury-kostek Apr 02 '19 at 10:34
  • 2
    If you really need to rename the prototype function, just assign it to something which suits you better like var h = Object.hasOwnProperty; – Krishna Prashatt Apr 02 '19 at 10:35
  • @ponury-kostek only if `null`, or `""` are not supposed to be valid values. They might be - you might have a `first_name` property but it's explicitly set to an empty value, which could be *different* to a missing property. – VLAZ Apr 02 '19 at 10:36
  • @VLAZ I think it's data from web form so those fields are always set and he want to check that someone fill them – ponury-kostek Apr 02 '19 at 10:40
  • @KrishnaPrashatt: That would require `Function.prototype.call` to invoke it, unless you did `var h = Function.call.bind(Object.hasOwnProperty);` – ziggy wiggy Apr 02 '19 at 10:42
  • @ponury-kostek if the fields are sent every time, then `hasOwnProperty` is redundant. It only makes sense to call it if they might not be there. – VLAZ Apr 02 '19 at 10:43

3 Answers3

2

You could just make a reference to the original hasOwnProperty method.

Object.prototype.h = Object.prototype.hasOwnProperty;

const data = {a:1};

if(data.h('a')){
  console.log('success');
}else{
  console.log('false');
}
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
  • 1
    I would make a reference to the original, but it's not wise to add that reference to the prototype - - changes to prototypes are discouraged. Just set a variable equal to `.hasOwnProperty`. – Scott Marcus Apr 02 '19 at 10:37
  • how would i set a variable equal to '.hasOwnProperty' ? – user1709076 Apr 02 '19 at 10:59
1

You can try with Object.prototype

Object.prototype.hop = function(p) {
  return this.hasOwnProperty(p);
}

Demo:

Object.prototype.hop = function(p) {
  return this.hasOwnProperty(p);
}

const object1 = new Object();
object1.property1 = 42;

console.log(object1.hop('property1')); // true
console.log(object1.hop('property2')); // false
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • thanks on nodejs i actually get an error when starting the server "the options [hop] is not supported" - not sure why – user1709076 Apr 02 '19 at 11:08
0

I'd just create a utility that receives a list of properties to check.

function hasProperties(obj, ...names) {
  return names.every(n => obj.hasOwnProperty(n))
}

 if(hasProperties(req.body, 'first_name', 'last_name'))
 {
    //....
 }

But .hasOwnProperty() calls are usually avoidable with shorter checks, unless you actually allow Object.prototype extensions in your code.

To answer the question directly, yes, but your desired solution would require such Object.prototype extensions. I think they're far more trouble than they're worth.

ziggy wiggy
  • 1,039
  • 6
  • 6