hasOwnProperty

931 views
Skip to first unread message

metamind

unread,
Jan 12, 2011, 8:54:17 AM1/12/11
to nodejs
Am I going mad? Shouldn't this work?

I am getting this error:
TypeError: Property 'hasOwnProperty' of object #<an Object> is not a
function

on this line:
if (gs.hasOwnProperty(key) && typeof(gs[key]) !== 'function') {

Oleg Slobodskoi

unread,
Jan 12, 2011, 9:00:49 AM1/12/11
to nod...@googlegroups.com
var test = {fui:123}; 

 if ( test.hasOwnProperty('fui') && typeof(test['fui']) !== 'function') {
   console.log(123);
}

works for me

2011/1/12 metamind <no...@craft-e.com>

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.




--
regards,
Oleg @oleg008

Adrian Olaru

unread,
Jan 12, 2011, 9:02:20 AM1/12/11
to nod...@googlegroups.com
I've also tested it on node edge / ubuntu 10.04...it works 4 me...

Vyacheslav Egorov

unread,
Jan 12, 2011, 9:02:43 AM1/12/11
to nod...@googlegroups.com
Yeah it should... Unless gs or something in it's prototype chain has
property 'hasOwnProperty' shadowing that of Object.prototype.

And something like:

console.log(gs.hasOwnProperty);

before the if.
--
Vyacheslav Egorov

metamind

unread,
Jan 12, 2011, 9:05:42 AM1/12/11
to nodejs
OK. I've got it.

I'm playing around with the global scope object of jslint. It derives
from the "standard" object (which contains a list of reserved words)
where "hasOwnProperty" is set to false.

Thanks anyway. It helps to know that normally it should have worked.

Jorge

unread,
Jan 12, 2011, 9:17:31 AM1/12/11
to nod...@googlegroups.com

Either
- gs is not an instanceof Object, or
- Object.prototype.hasOwnProperty is being shadowed.
--
Jorge.

Tom Robinson

unread,
Jan 12, 2011, 7:14:21 PM1/12/11
to nod...@googlegroups.com
This is the safest way to use hasOwnProperty:

Object.prototype.hasOwnProperty.call(object, property)

It's also especially important to do if "hasOwnProperty" itself could be a key in your object...

var set = {};
set["hasOwnProperty"] = "blah";
...
set.hasOwnProperty("foo")

Oops.

Nick Husher

unread,
Jan 13, 2011, 2:19:37 AM1/13/11
to nod...@googlegroups.com
This is the safest way to use hasOwnProperty 
 
Object.prototype.hasOwnProperty.call(object, property)

That can certainly get wordy, but you can always wrap it in a util function:

function hasOwn(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }

Also, it's worth noting that the most common use of hOP is in a loop, which is made much simpler in ECMA5. Before:

for(var key in object) {
  if(object.hasOwnProperty(key)) {
    var prop = object[key];
    // ...
  }
}

Now you can do:

Object.keys(object).forEach(function(key) {
  var prop = object[key];
  // ...
}, this);

Somewhat clearer, free of unnecessary indentation, and gives you a little more flexibility to operate on your key array via chaining before looping over it. Also it introduces a new function context so you can declare loop variables without worrying about clobbering those in your containing function.

In some cases you may want to replace Object.keys with Object.getOwnPropertyNames, which has some subtle differences.

Tim Caswell

unread,
Jan 14, 2011, 2:47:37 AM1/14/11
to nod...@googlegroups.com
Also, it's important to note that Object.keys and Object.getOwnPropertyNames are a lot faster than a for..in loop because they can ignore the inheritance chain.

It's actually faster to do:

Object.keys(obj).forEach(function (key) 

than

for key in obj { ... }

The fastest is:

var keys = Object.keys(obj);
for (var i = 0, l = keys.length; i < l; i++) { 
  var key = keys[i], value = obj[key]; ...
}

but that's ugly and drops variables all over the current scope (for doesn't create a new scope)

If you like to change built-in prototypes you can implement a Object.prototype.forEach that internally uses Object.keys and for(;;;) and it will be faster than everything except itself inlined.

obj.forEach(function (value, key) {...})

Or make it an external utility function to keep builtins pristine

forEach(obj, function (value, key) {...})

Reply all
Reply to author
Forward
0 new messages