Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 92
    Another way to iterate only over "own" properties is Object.keys. Object.keys(target).forEach(function (key) { target[key]; });. Commented Aug 30, 2011 at 10:50
  • 5
    not going to work if target is created using Object.create(null), code should be changed target.hasOwnProperty(k) -> Object.prototype.hasOwnProperty.call(target,k) Commented Sep 30, 2015 at 9:49
  • 1
    why not to use variables given in question example? What here is k, target and property? For me, non-javascripter this area of undefined :) Commented Mar 30, 2017 at 13:24
  • 2
    Object.keys(target).forEach((key) => { target[key]; }); for Angular Commented Aug 18, 2017 at 17:30
  • I would say that almost every time you see obj.hasOwnProperty(k) it should be rewritten as Object.prototype.hasOwnProperty.call(obj, k). If you don't know whether or not an object has an own property k, then you probably also don't know for sure whether it has an own property named "hasOwnProperty"; and if it does, you don't want that one, you want the one from Object.prototype. So IMO making hasOwnProperty a method at all was a design flaw in the language; nobody ever wants its behaviour to be overridden in practice. Commented Jan 17, 2022 at 1:56