Lasse Reichstein:

> You can use either Object.getPrototypeOf or __proto__ (with a preference on
> the other, because it's not as easily modified) to check:
>    prop in object && !(prop in object.__proto__)
> which is not same check as hasOwnProperty (if the property is both on the
> object and also in its prototype chain), but might work in cinch.

In general interesting solution. Unfortunately it should be mention
that it won't work in case of shadowing property from the prototype
chain.


var obj = Object.create({foo: "Inherit"});

obj.foo = "Own";

"foo" in obj && !("foo" in obj.__proto__) //false

If there is mutable `__proto__' he is able to truncate the prototype
chain, check with `in` and then restore the state of the prototype
chain.

function hasOwnProp(o, p) {
    var proto = o.__proto__,
        res;

    o.__proto__ = null;
    res = p in o;
    o.__proto__ = proto;

    return res;
}

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to