user3342816
user3342816

Reputation: 1263

Proper property loop of objects

OK. So until now I have "gotten away" with using things like this:

function foo(arg) {
    // Default values:
    var opt = { a: 1, b: 2, c: 3 }, 
        key;
    for (key in opt)
        if (opt.hasOwnProperty(key) && arg.hasOwnProperty(key))
           opt[key] = arg[key];
}

Now, I'm more and more bashed for using hasOwnProperty as it could have been overridden by some custom property.

If we are going to use keys(), forEach() etc. instead what is the proper way of doing things like the above? Something like this?

function bar(arg) {
    // Default values:
    var opt = { a: 1, b: 2, c: 3 }, 
        keys = Object.keys(arg);
    Object.keys(opt).forEach(function(key) {
        if (keys.indexOf(key) !== -1)
            opt[key] = arg[key];
    });
}

Upvotes: 1

Views: 79

Answers (2)

xdazz
xdazz

Reputation: 160863

If you are concerning hasOwnProperty has been overridden, then you could do:

Object.prototype.hasOwnProperty.call(arg, key)

So your original code could be:

function foo(arg) {
    var opt = { a: 1, b: 2, c: 3 },
        check = Object.prototype.hasOwnProperty,
        key;
    for (key in opt) {
        if (check.call(opt, key) && check.call(arg, key)) {
            opt[key] = arg[key];
        }
    }
    return opt;
}

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1074505

There's nothing wrong with your original code — except for the shocking lack of { and }! ;-)

If you feel for some reason you need to use Object.keys and forEach instead, you don't need to get the keys of arg, just opt. This would be the equivalent of your first code block, using Object.keys and forEach.

function bar(arg) {
    // Default values:
    var opt = { a: 1, b: 2, c: 3 };
    Object.keys(opt).forEach(function(key) {
        if (arg.hasOwnProperty(key))
            opt[key] = arg[key];
    });
}

But again, there's no reason to change it, your original code is fine.

Now, I'm more and more bashed for using hasOwnProperty as it could have been overridden by some custom property.

If someone overrides hasOwnProperty, then in theory they have a good reason for doing so. I wouldn't worry about it except in some special situations. But if you want to avoid that concern, you can do this:

var hop = Object.prototype.hasOwnProperty;
function foo(arg) {
    // Default values:
    var opt = { a: 1, b: 2, c: 3 }, 
        key;
    for (key in opt)
        if (hop.call(opt, key) && hop.call(arg, key))
           opt[key] = arg[key];
}

Then your only concern is whether somebody replaced Object.prototype.hasOwnProperty; if they did, and their replacement didn't work properly, your loop would probably be the least of their concerns.

Upvotes: 1

Related Questions