我不完全確定如何在JS中實現OOP概念。JavaScript:公共方法和原型
我這是在它的構造完全聲明的類:
function AjaxList(settings)
{
// all these vars are of dubious necessity... could probably just use `settings` directly
var _jq_choice_selector = settings['choice_selector'];
var _jq_chosen_list = settings['chosen_list'];
var _cb_onRefresh = settings['on_refresh'];
var _url_all_choices = settings['url_choices'];
var _url_chosen = settings['url_chosen'];
var _url_delete_format = settings['url_delete_format'];
var jq_choice_selector_form = _jq_choice_selector.closest("form");
if (DEBUG && jq_choice_selector_form.length != 1)
{
throw("There was an error selecting the form for the choice selector.");
}
function refresh()
{
_updateChoicesSelector();
_updateChosenList();
_cb_onRefresh();
};
AjaxList.prototype.refresh = refresh; // will this be called on all AjaxLists, or just the instance used to call it?
// AjaxList.refresh = refresh; // will this be called on all AjaxLists, or just the instance used to call it?
// ...
}
有AjaxList的多個實例。當我在其中一個電話上撥打refresh()
時,我只想要一個列表來刷新自己。在以下情況下:
term_list = AjaxList(settings);
term_list.refresh();
調用refresh()
調用似乎使所有AjaxLists自己刷新。什麼是正確的方法來做到這一點?
我使用jQuery,如果它有任何區別。
你對語言有一些嚴重的誤解(javascript,jQuery,class)。看到我的答案,看看他們清楚。 – galambalazs 2010-07-05 19:47:23