【发布时间】:2013-04-16 22:02:24
【问题描述】:
我承认我对 javascript、jquery 或 oop 知之甚少,但我知道的足够多,可以拼凑一些东西,有时还能让它们工作。然而,这超出了我的想象,尽管我用谷歌搜索,但我无法对正在发生的事情做出正面或反面。这是我的代码的要点:
jQuery(document).ready(function($) {
var methods = {
init : function( options ) {
if (somthing) {
this.latlng(input);// <--- ERROR: Object has no method
}
},
auto : function( ) {
if (something) {
this.latlng(input);
} else {
this.location(input);
}
},
location : function ( input ) {
// draw map
},
latlng : function ( input, l ) {
// draw map
}
}
$.fn.codeAddress = function( method ) {
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tooltip' );
}
};
var geo = new $(document).codeAddress(); // Initialize the object
});
我依靠 jQuery Docs Plugin/Authoring 作为我的模板,并从那里开始零敲碎打。理想情况下,它会在文档准备好时自行加载 init(),但它不会,所以我添加了倒数第二行来初始化对象。
地图最初是在 init() 中使用方法 latlng() 来绘制的。这就是我在第 6 行得到错误的地方,this.latlng(input) Uncaught TypeError: Object [object Object] has no method 'latlng'。此后,onclick 事件处理程序调用 auto() 以根据接收到的输入重绘地图。
如果我的解释和/或代码是垃圾节目,我深表歉意。我正在努力学习。
【问题讨论】:
-
@adeneo,OP 已经明确表示他对 javascript 了解不多。做个好人;)
标签: javascript jquery function methods