我有这个代码:
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");}
}
但是当我调用 Flipppable.foo() 时,我得到了未定义的结果。有什么想法我的语法有问题吗?
谢谢!
克里斯
** 更新 **
我正在创建一个像这样的插件:
$.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 );
未捕获类型错误:无法调用未定义的“apply”方法 } 别的 { $.error( '方法 ' + 方法 + ' jQuery.flippable 上不存在' ); }
};
它上线了:
return Flippable._init.apply( this, arguments );
_init 未定义。
最佳答案
原型(prototype)函数只能通过类的对象访问。而不是类(class)本身。 这会起作用。
var oFlip = new Flippable();
oFlip.foo();
关于javascript - 无法使用 Prototype 扩展我的 javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16120383/