0

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.

1
  • 1
    Have you created an object with new Flippable()? PS: it's common to modify prototype, not completely override. Commented Apr 20, 2013 at 12:32

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

They are also accessible through the prototype object of Flippable since this is where they are declared.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.