Greg, you can't extend Object.prototype. It will break more than just
jQuery. As you noted, there is a workaround, but it requres code to be added
to every for loop, which has a performance impact.

Instead, simply revise the Object.prototype.Inherits function from that page
so it does not need to be an Object method.

Change this code:

    Object.prototype.Inherits = function( parent ) {
        if( arguments.length > 1 )
            parent.apply( this, Array.prototype.slice.call( arguments, 1 )
);
        else
            parent.call( this );
    };

to:

    function InheritObject( self, parent ) {
        if( arguments.length > 1 )
            parent.apply( self, Array.prototype.slice.call( arguments, 1 )
);
        else
            parent.call( self );
    };

And where you code:

    this.Inherits( Mammal, name );

Change it to:

    InheritObject( this, Mammal, name );

Problem solved.

The Function.prototype.Inherits method is not as much of a problem. It won't
break any code unless you include some other code that also uses the same
method name for a different function.

If that's a concern, you could give that function the same treatment. You
may want to do this just for consistency anyway.

Change:

    Function.prototype.Inherits = function( parent ) {
        this.prototype = new parent();
        this.prototype.constructor = this;
    };

To:

    InheritFunction = function( self, parent ) {
        self.prototype = new parent();
        self.prototype.constructor = self;
    };

And change:

    Cat.Inherits( Mammal );

To:

    InheritFunction( Cat, Mammal );

-Mike

> -----Original Message-----
> From: jquery-en@googlegroups.com 
> [mailto:[EMAIL PROTECTED] On Behalf Of Greg
> Sent: Tuesday, July 22, 2008 3:36 AM
> To: jQuery (English)
> Subject: [jQuery] Augmentation of Object.prototype
> 
> 
> Hi everyone,
> I want to use the code from this page :
> http://www.coolpage.com/developer/javascript/Correct%20OOP%20f
> or%20Javascript.html
> to enable true inheritance with JavaScript ... but sadly 
> JQuery doesn't support a such possibility.. Why ?
> 
> If you take care by using "hasOwnProperty()" when you iterate 
> to an object you will never reach my added functions. Like that :
> for(var m in o) {
>    if (m.hasOwnProperty()) {
>       // use of m..
>    }
> }
> 
> Or better with a such augmentation ;o) :
> Object.prototype.each = function(f) {
>    for (var k in this) {
>       if (this.hasOwnProperty(k)) {
>          f(k, this[k]);
>       }
>    }
> };
> 
> Is there an explanation of this limitation ?
> 
> TIA
> /Greg
> 

Reply via email to