On 10 Mar, 07:25, Jim Higson <[email protected]> wrote:
> On Monday 09 March 2009 21:51:05 kangax wrote:
>
>
>
> > > > It's possible that we'll introduce a custom `hasOwnProperty` in later
> > > > revisions.
>
> > > Makes sense.
> > > if( !hasOwnProperty )
> > > hasOwnProperty = function hasOwnProperty(){ ... };
>
> > I was thinking of something like:
>
> > var hasOwnProperty = (function(){
> > var hop = Object.prototype.hasOwnProperty;
> > if (hop) {
> > return function(obj, prop) {
> > return hop.call(obj, prop);
> > }
> > }
>
> Why not:
> if( hop )
> return hop;
>
> Does hop need to be wrapped?
Writing `hasOwnProperty.call(object, property)` is a bit tedious. Why
not just encapsulate the `call`?
>
> > return function(obj, prop) {
> > if (obj) {
> > var c = obj.constructor;
> > if (c && c.prototype) {
> > return obj[prop] !== c.prototype[prop];
> > }
> > }
> > return null;
> > }
> > })();
>
> > This fallback not bullet-proof, but should cover most of the cases.
>
> Yep, the tragic case where something just happens to have a property in a
> prototype *and* the same property directly - could happen! Still, I think the
> code above would be fine for almost all cases.
Another edge case is when an object does not expose (think host
objects in MSHTML DOM) or is missing `constructor` altogether (e.g. if
it was overwritten). I remember seeing solutions involving deleting
object's `__proto__` (if supported), checking property with `in` and
then restoring `__proto__` back. Relying on such manipulations is
like playing with fire, of course : ) It is probably also slower.
--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---