JavaScript:公共方法和原型
我不完全确定如何在 JS 中实现 OOP 概念。
我有一个在其构造函数中完全声明的类:
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()
调用似乎使所有 AjaxList 自行刷新。这样做的正确方法是什么?
我正在使用 jQuery,如果它有什么区别的话。
I'm not entirely sure how to implement OOP concepts in JS.
I have a class which is entirely declared in its constructor:
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?
// ...
}
There are multiple instances of AjaxList. When I call refresh()
on one of them, I want only that one list to refresh itself. In the following instance:
term_list = AjaxList(settings);
term_list.refresh();
The refresh()
call seems to make all the AjaxLists refresh themselves. What is the correct way to do this?
I'm using jQuery, if it makes any difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该在构造函数中重新定义原型函数。
如果要创建特权函数,请在构造函数中使用 this.methodname = ... 。
另外,如果你想创建一个合适的对象,你需要更改
为
You should not redefine the prototype function in the constructor.
If you want to create a privileged function use this.methodname = ... from the constructor.
Also if you want to create a proper object, you need to change
to
鉴于该结构,您应该能够调用:
Given that structure, you should be able to call:
不,没有。在这里查看我的答案: Javascript、Jquery 和 Ajax 之间有什么区别?
Javascript 中没有类。忘记他们吧。您确实需要学习该语言的一些基础知识才能使用它们。它不是 Java,尽管它看起来很相似。
如果您有构造函数,它将创建一个实例。 共享方法将位于原型链中,并且只有特定于实例的数据才直接进入具有这个关键字。
因此,对象的基本概念如下所示:
这个概念的力量在于内存中只有一个刷新函数。它可以处理任何情况。此外,如果另一个对象继承自MyObject,则刷新功能将被继承。但内存中仍然会有一个共享刷新功能。它可以处理任何父实例或子实例。
No it doesn't. See my answer here: What's the difference between Javascript, Jquery and Ajax?
There are no classes in Javascript. Forget them. You really need to learn some of the basics of this language in order to use them. It's not Java, even though it looks similar.
If you have a Constructor Function it will create an instance. The shared methods will be in the prototype chain, and only instance specific data goes right into the function with the this keyword.
So the basic concept of an object would look like this:
The power of this concept is that there is only one refresh function in memory. And it can deal with any instance. In addition, if another object inherits from MyObject the refresh function will be inherited. But in the memory there will be still one shared refresh function. And it can deal with any of the parent or child instances.