Mister Epic
Mister Epic

Reputation: 16713

Cannot extend my javascript object with Prototype

I have this code:

var Flippable = function( options, element ) {
    this.$el    = $( element );
    this._init( options );
};

Flippable.prototype     = {
    _init               : function( options ) {

        this.options        = $.extend( true, {}, $.Flips.defaults, options );
        this.$pages         = this.$el.children( 'div.f-page' );
        this.pagesCount     = this.$pages.length;
        this.History        = window.History;
        this.currentPage    = this.options.current;
        this._validateOpts();
        this._getWinSize();
        this._getState();
        this._layout();
        this._initTouchSwipe();
        this._loadEvents();
        this._goto();

    },
    foo: function(){alert("foo");}
}

But when I call Flipppable.foo(), I get undefined. Any ideas where my syntax is off?

Thanks!

Chris

** Update **

I"m creating a plugin like so:

$.fn.flippable = function( method ) {

// Method calling logic
if ( Flippable[method] ) {
  return Flippable[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
  return Flippable._init.apply( this, arguments );

Uncaught TypeError: Cannot call method 'apply' of undefined } else { $.error( 'Method ' + method + ' does not exist on jQuery.flippable' ); }

};

It borks on line:

return Flippable._init.apply( this, arguments );

_init is undefined.

Upvotes: 0

Views: 84

Answers (2)

Jacob T. Nielsen
Jacob T. Nielsen

Reputation: 2978

The way you are trying to call foo using .apply you will have to do this.

Flippable.prototype.foo.apply( this, arguments );

The object which has a foo property is prototype on Flippable and not Flippable itself.

Upvotes: 0

Parthik Gosar
Parthik Gosar

Reputation: 11646

Prototype functions are accessible only through the object of the class. And not the class itself. This would work.

var oFlip = new Flippable();
oFlip.foo();

Upvotes: 2

Related Questions